Android Proximity Sensor As Clear As Possible
Proximity sensor can detect a short distance (~5cm)
Proximity sensor is a short-distance detection component. The maximum distance is approximately 5cm. Google provides SensorManager
to handle the operation between app and hardware sensors. Developer can use the distance information to provide a better UX, e.g. dimming the screen when user is answering a phone call.
Moreover, there are mainly two types of proximity sensor in the market. They can be either embedded into the bezel or put under the screen.
The implementation of proximity sensor in Android is simple and only consists of only 3 steps! Let’s dive into it.
All-In-One code
Step-by-Step instructions
Step 1: Get the SensorManager system service
SensorManager
is an Android system service. We can use the getSystemService()
to retrieve it easily.
Step 2: Access to proximity sensor through SensorManager
However, proximity sensor is only an optional component in modern Android device, especially for tablet. We have to add a null checking to it to prevent any runtime error!
Step 3: Register the SensorEventListener
SensorEventListener
is a general listener to all hardware sensor. Different sensors would return different kinds of data back. According to official documentation, the distance detected by proximity sensor is stored at the first element of values
field in the returned SensorEvent
object.
Build and run you app. The above log would be shown if you move your hand to and from the proximity sensor continuously.
By the way, not all proximity sensors would return the exact detected distance back. Some of the sensors would return a binary value to show whether it is a “near” or “far” value.
Accuracy
Sensor can have different sensitivities and accuracies at different situations, e.g. temperature. We have to filter out those inaccurate data from raw data.
There are totally five possible accuracy levels returned from SensorEventListener
:
SENSOR_STATUS_ACCURACY_HIGH
Sensor is operating with its maximum accuracy and the data is the most reliable.SENSOR_STATUS_ACCURACY_MEDIUM
Accuracy is still acceptable but calibration is recommended to increase the accuracy.SENSOR_STATUS_ACCURACY_LOW
Error is high and calibration is highly recommended.SENSOR_STATUS_UNRELIABLE
Data should not be trusted since sensor is not operating in its normal way.SENSOR_STATUS_NO_CONTACT
(Not applicable to proximity sensor) Sensor is not contacting with what it is measuring to.
Therefore, the onSensorChanged
callback should be updated to above code for more reliable data.
Maximum distance
As mentioned above, the proximity sensor can detect a distance for around 5cm. However, different sensors would have different ranges. You can get the maximum distance by:
I have run a sample app to the following devices and here are the results:
- Samsung Galaxy Nexus (2012): 5cm
- Samsung Galaxy Note 5 (2015): 8cm
- Pixel 3a (2019): 5cm
You probably would like the following article:
Summary
- Proximity sensor is used to detect a short distance for around 5cm. Some devices can even detect up to 8cm far.
- We can use
SensorManager
to access to the proximity sensor and retrieve the detected distance at theonSensorChanged
callback ofSensorEventListener
. - Different Proximity sensor can have different accuracy at different situations. A good developer should filter out those data with unacceptable accuracy.
You are welcome to follow me at Twitter@myrick_chow for more information and articles. Thank you for reading this article. Have a nice day! 😄