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)
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).