Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect user proximity from the screen

I'm working on a BB10 app that needs to be able to disable the screen when the user holds the phone up to his/her face during a call.

How can I tell when the user is holding the phone up to his/her face?

like image 316
Brian Avatar asked Oct 20 '22 14:10

Brian


1 Answers

To detect the user's proximity from the phone, you can use a QProximitySensor.

In order to use this, you need to add these lines to your project's .pro file:

CONFIG += mobility
MOBILITY += sensors

Add the necessary includes to the .cpp and .h files:

#include <QtSensors/QProximitySensor>
using QtMobility::QProximitySensor;

#include <QtSensors/QProximityReading>
using QtMobility::QProximityReading;

Define the proximity sensor in the .h file. Create and destroy the sensor in your constructor and destructor functions.

When the call starts, connect your sensor's readingChanged function to the one that you intend to handle the reading, and activate it:

connect(proximitySensor, SIGNAL(readingChanged()), this, SLOT(checkReading()));
proximitySensor->setActive(true);

When the call ends, deactivate the sensor:

proximitySensor->setActive(false);

Finally, use the reading's close function to tell when the device is close to the user's face. Note that the distance defined as "close" may be different for different devices.

bool isClose = proximitySensor->reading()->close();

Alternatively, if you don't want to act upon changes to the reading, you can skip connecting the readingChanged signal and use the close function independently.

like image 104
Brian Avatar answered Oct 22 '22 21:10

Brian