Proximity sensor in android apps

The Android platform provides a sensor that lets you determine how close the face of a device is to an object (known as the proximity sensor). Proximity sensor is hardware based, we might often experience that our phone screen would turn off when we bring the phone to our ears when we are in a call and the screen will turn on when we take it back. This is because the proximity sensor recognises the object near the phone.

Now let see how we can use this sensor in our app to make it more interactive.

First of all need to add <uses-feature> in AndroidManifest.xml before start using this sensor, but it is not mandatory, with this devices without proximity hardware will not able to install.

<uses-feature android:name="android.hardware.sensor.proximity" android:required="true" />

Now we need to instantiate Sensor object of  TYPE_PROXIMITY.

sensorManager =  (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

Once the Proximity sensor object is initialised we need to register listener to listen the changes in values given by this sensor.

sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);

After registering the listener, sensor starts returning the values in the methods of the listener as follows –

@Override
public void onSensorChanged(SensorEvent event) {
    if(event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
        if(event.values[0]==0){
              //represents object is near to device
         }else{
              //represents object is far to device
         }
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

 

The range of proximity sensor is still not fixed , it varies from one manufacture to another, but we can assume that the MINIMUM value is 0 and MAXIMUM value can be determined by the following method of the sensor object

sensor.getMaximumRange();

So based on the minimum and maximum value you can determine the behaviour of your android app.

 

Once you are done with the sensor ***do not forget to unregister*** the listener,  as listening sensors are battery consuming, so to unregister call the following method once you are done with it or once the activity/fragment get destroyed.

sensorManager.unregisterListener(this);

 

Here is the full class description to implement proximity sensor in an activity –

public class MainActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private Sensor sensor;
private TextView  textView;

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sensorManager =  (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    textView = (TextView) findViewById(R.id.textview);

    if(sensor == null) {
        textView.setText("No Proximity Sensor found");
    }else{
        textView.setText(sensor.getName() + " Found");
    }
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY), SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onResume() {
    super.onResume();
    sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event) {
    if(event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
        if(event.values[0] == 0) {
 
        }else{
 
        }
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

After all of this you might have the question that what are the applications of this proximity sensor, so we can use this for several scenarios like –

 

  • during call turning on/off phone screen
  • during video playing play/pause by putting hands over the screen
  • or might be something else

So implementation of proximity sensor in your app might be game changer.

For more information follows the below links –

https://en.wikipedia.org/wiki/Proximity_sensor

http://stackoverflow.com/questions/tagged/proximitysensor

 

Hopefully this article is useful for many of you, do not forget to share with your friends and colleagues.

Happy coding !!!

Note : photo is taken from flickr

Leave a Reply

Your email address will not be published. Required fields are marked *