The proximity sensor is common on most smart-phones, the ones that have a touchscreen. This is because the primary function of a proximity sensor is to disable accidental touch events. The most common scenario being- The ear coming in contact with the screen and generating touch events, while on a call.
To solve the "I didn't take that stupid picture, my ear did" issue, device manufacturers came up with the idea of a placing a proximity sensor close to the speaker, which will then detect any object in the vicinity of the speaker. If any object is present (ex. user's ear), then the touch events can be assumed to be accidental & ignored.
Now there are various technologies for proximity sensing:
- Electrical (Inductive, Capacitive)
- Optical. (IR, Laser)
- Magnetic.
- Sonar.
Of all these, the most non-intrusive and low-cost modules are the optical proximity sensors. These can detect bodies in the vicinity of the device upto 5cm. This is perfect for use on smart-phones.
Coming to Sensors on Android-Gingerbread, the proximity sensor is often implemented using a light sensor chip. Common ones are ISL29003/23 & GP2A by Intersil & Sharp respectively. Both these sensor-chips are primarily active light sensors, which provide the ambient light intensity in LUX units.
The distance value is measured in centimeters. Note that some proximity sensors only support a binary "close" or "far" measurement. In this case, the sensor should report its maxRange value in the "far" state and a value less than maxRange in the "near" state.
Let us take an example. Say the device uses a GP2A chip placed close to the speaker facing the user (as shown in the adjoining pic). The normal light response of the GP2A is such that it triggers a proximity-detect interrupt at a distance of approximately 5cms in normal lighting.
This means that when the user receives a call and brings the phone closer to his ear, the ambient-light around the light/proximity-sensor slowly drops down below the threshold & the sensor detects this and switches the state from FAR to NEAR. This event can be detected by any android application which has registered a SensorEventListener.
In most Android phones, the proximity sensor is implemented as a boolean-sensor. Its returns just two values "NEAR" & "FAR". Thresholding is done on the LUX value i.e. the LUX value of the light sensor is compared with a threshold. A LUX-value more than threshold means the proximity sensor returns "FAR". Anything less than the threshold value and the sensor returns "NEAR". The actual value of threshold is custom-defined depending on the sensor-chip in use and its light-response, coupled with the location & orientation of the chip on the smart-phone body.
The proximity sensor is implemented very much like the other sensors except on one critical point -
Proximity sensor is interrupt-based (not Poll-based).
All the other sensors are polled for at regular intervals, selected by the DELAY parameter used while registering a SensorEventListener. Proximity Sensor is interrupt based (NOT polling). This means that we get a proximity event only when the proximity changes (either NEAR to FAR or FAR to NEAR).
__________________________
Further Reading:



Really an useful info.
ReplyDeleteNice informative piece! Thank you!
ReplyDelete"A LUX-value more than threshold means the proximity sensor returns "FAR". Anything less than the threshold value and the sensor returns "NEAR"." is one of the methods of implementation of proximity sensor.
ReplyDeleteIf the above method as described is used in smartphones, how does the proximity feature work on 'call' during nights ?
During night, ambient light sensor detects very low lux value which falls below the threshold value. But this shouldnt trigger a "NEAR" interrupt when the phone is away.
A very good point.
DeleteThis situation is handled by "active" proximity-sensors that have a light-source embedded in the module itself. This is usually an LED that emits light in the Infrared(non-visible) spectrum.
An object in the near vicinity of the proximity-sensor reflects a good amount IR light back to the sensor. This is used to signal a "NEAR" interrupt. As the object is moved away, the amount of IR light reflected back reduces. Once this falls below a threshold, the "FAR" interrupt is raised.
Detailed diagram:
Proximity sensor hardware on Android.
nice info. thanku....
ReplyDeletethank u.. very useful point
ReplyDeletevery useful,thank u....
ReplyDeleteIs there any way to restart the service or process that runs the proximity sensor on android 2.3.5? reason i ask is, whenever i make a bluetooth connection, then disconnect the connection, and return to normal phone-to-face use, the sensor no longer operates until i reboot the phone.
ReplyDeleteThe sensorservice would be a thread running as part of systemserver. Nothing short of a reboot (or killing systemserver, so that it restarts automatically) will "reset" this. This doesn't seem to be a standard issue though. GB2.3.7 on N1 seems to do fine in the above scenario. May i know the device/ROM where you see the issue?...
ReplyDeleteI am coding an Android App that uses ambient light sensor. My problem is: when my java code is checking for the light sensor, it says that their is "no light sensor present". However, my phone has an ambient light sensor, and it works fine. Can someone help me out? maybe i missed some permission on the manifest file.
ReplyDeleteThanks in advance...
On some devices the ambient-light-sensing hardware is present but the kernel-driver & sensor-HAL are written to provide only a boolean proximity-sensor as this is sufficient to disable/enable the screen during a call.
DeleteCan you call getSensorList() in your application and see if the reported list contains a light sensor?
Which Android phone/tablet are you trying this out on?
Thanks for that info Chinmay,
DeleteI haven't tried that yet but i tried running it on LG E400 and samsung galaxy y and experiencing the same trouble.
Unfortunately both LG-E400 and Samsung Galaxy-Y provide ONLY proximity-sensor data with the default factory-ROM.
Deletehow can i deal with that? i mean, what should i use in my code? how can i use its light sensor for the brightness of the screen?
Deletebtw
I am using Sony Ericsson ST15i, on its technical specs, it has an ambient light sensor.
A quick and definitive way of testing for ALS support on any device would be calling getSensorList() in an app(sensors-release.apk) on the device. ALS will be supported using Android sensor APIs if and ONLY if a "light-sensor" returning LUX values is listed in the list of sensors getSensorList returns.
DeleteIf present one can then go ahead and obtain an handle to the light-sensor using standard Android sensor APIs and carry on from there.
If NOT present, then the options would be to see if the OEM has devised any alternative methods(cat sysfs etc.). If not, then searching for custom kernel/ROMs with ALS support for the device is the last alternative. This in turn will require rooting the device.
Try running sensors-release.apk on the device. The app calls getSensorList() and displays the list of sensors detected on the screen.
Thanks again Chinmay,,, ill use this app to check. Ill get back to you.
DeleteAnother thing Chinmay, if I root my phone, should i need to change my java code? I mean is their a different
Deletemethod to ACCESS that said light sensor?
Apart from the android sensor APIs there is no documented standard across devices/ROMs.
DeleteAFAIK, there are very few Android devices that do provide ALS. Most low-end devices just provide a proximity sensor(though on some the hardware supports proximity+ALS).
IF a device does NOT list a light-sensor in getSensorList(), but provides ALS functionality like automatic brightness adjustment of the display-backlight, then it would mean that a custom method has been implemented on the device/ROM for the light-sensor. To use this one would definitely need to change the java code in the app.
Now here is the deal(LOTs of work):
+ IF device has ALS hardware,
+ IF ALS hardware datasheets are available,
+ IF device is rooted,
+ IF device android/kernel source is available,
Then one could actually go ahead and implement the ALS functionality in the kernel-driver and the sensor-HAL. The software support for sensors is present in the sensor-HAL on the device:
/system/lib/hw/sensors..so
This would need to be modified and rebuilt. Then once the modified kernel and sensor-HAL are copied onto the device, (which is what custom-ROMs developers do) getSensorList() will list a light-sensor and standard Android sensor APIs will work properly i.e. no change is java code.
Thank you so much for that info and for the support...
ReplyDeleteIt is a very nice blog posted that provide me information regarding the proximity sensor on Android gingerbread and i know how it works. So thanks for sharing this kind of information.
ReplyDeleteGreat info! keep up the good work and knowledge sharing..
ReplyDeleteThanks for the info! i didn't know what it was for, now i know thx!
ReplyDeleteGreat information about Proximity Sensor.
ReplyDeleteI use it for lock/unlock screen on my phone using an Android App. It works like a charm.
Again Thanks
Hi brother just a quick advise m using samsung note 2 and I have a red dim light on the top of my hp is that proxm.device as mention and how do we switch off whats ur advise what are the pros n cons of these app. Thank u
ReplyDeleteAre you sure its not the notification LED that is blinking red? Note2 does have a notification LED near the front-cam and the proximity sensor. You can use apps like "Lightflow" to configure the notification LED.
Delete