Thursday 26 February 2015

Proximity Sensor in Android Part-1

Proximity Sensor
It is one of the sensor which is available in almost all android devices.This sensor detects the near by objects and send a signal to the android device which you can be catched by Sensor framework. API provides by Sensor framework, you can use inside your application. The object that this sensors can detect is anything like human body part (hand, finger, ear etc) or any other objects.


Location of proximity sensor on your device


This is generally located in the upper part of device display area near the front speaker. Most of the device, this sensor gives only two values "NEAR" and "FAR". "NEAR" is approximately less than 5 cm. This is an event based sensor, so this sensors produce data only when there is a change it its values from NEAR to FAR and vice-versa. On some devices you will get float value.
Whenever you are on a "call" you generally put it near your ear, this sensors detects the ear and sends the signal that something is near its proximity. At that point of time many UI touch feature is disabled so that if some actionable buttons (stop call button) is touched even unintentionally, it will not be activated to close the call.This is one the main feature of this sensor.


How to use it ?


You can use this sensor in your application as a trigger point or as an event and can perform some funny action or the actions you wants.


First of all you need to check whether this sensor is available on your device or not...Most of the cases it will be for sure. Next, After checking that it is available in your device make a listener class which listens for this sensor events. You have to implements the SensorEventListener interface and implements the methods inside the listener class. Inside the onSensorChanged method you can capture the event values. Use this values according to your application requirements.


How to test?


To test your application for the sensor you need a physical device not an emulator.


1. Get an instance of SensorManager
1a. Get Available Sensor List
1b. Get a reference of Proximity Sensor
3. Implement SensorListener interface
4. Register your Listener
5. Unregister the Listener

public class ProximityActivity extends Activity implements SensorEventListener {
   SensorManager sManager;
   Sensor proximity;    

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_proximity);
       sManager=(SensorManager)getSystemService(SENSOR_SERVICE);
       proximity=sManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);       
       if(proximity!=null){
       Log.i(“Proximity”,”Device has Proximity Sensor”);
   }
   @Override
   protected void onResume() {
       super.onResume();
       sManager.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
   }
   @Override
   protected void onPause() {
       super.onPause();
      sManager.unregisterListener(this);
   }

   @Override
   public void onSensorChanged(SensorEvent event) {                
    // use the sensor value here...it may be near or far
    // or value in cm then you can define your own value like below
     float value_0 = event.values[0];
           String proximity;
           if(value_0<=5.0)
               proximity="NEAR";
           else
               proximity="FAR";   
   }

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


In the next part, we will create a live example using proximity sensor…….;)

No comments:

Post a Comment