Using proximity sensor in Android App

Proximity Sensor in Android device is really helpful while building interactive applications, it can help you to add very good usability features in application.

For more technical details of proximity sensor and how it works check Wikipedia.

The main function of proximity sensor is to detect presence of nearby objects without any physical contact.

Now in many interactive apps it can be used like :

– Receive a call when user picks up the device and takes it near their ear
– Automate audio recording when user takes device near their ear
– Reject call when phone is placed upside down
– Turnoff screen when phone is in pocket
– Turnoff screen when phone is near ear to avoid accidental touches

We used this feature for our app to start playing a sound through earpiece when user takes device near their ear. It took some time to get it working so we thought to share the code so it can be helpful for others, you can use below code to get it working easily.

Usage : 
/**
 * Initial set Up of proximity sensor
 *  
 * It will call onPlay when device near to any object,
 * and will call onPause when device os moved far from the object
 */
private void setUpProximitySensor()
{
    if(mProximityController == null)
    {
        mProximityController = new ProximitySensorController(getApplicationContext()
        ,new onProximitySensorCallback() {
            
            @Override
            public void onSensorUnregister() 
            {
                Log.i(TAG,String.format("onSensorUnregister"));
            }
            
            @Override
            public void onSensorRegister() 
            {
                Log.i(TAG,String.format("onSensorRegister"));
            }
            
            @Override
            public void onPlay() 
            {
                onPlayPauseCall(false);
            }
            
            @Override
            public void onPause() 
            {
                onPlayPauseCall(true);
            }
            
            @Override
            public void onError(int errorCode, String message) 
            {
                Log.e(TAG,String.format("onError() : %2s", message));
            }
        });
        
        //start register here
        registerProximitySensor();
    }
}


/**
 * register the proximity sensor using proximity controller
 */
private void registerProximitySensor()
{
    if(mProximityController != null)
    {
        mProximityController.registerListener();
    }
}

/**
 * Unregister the proximity sensor using proximity controller
 */ 
private void unregisterProximitySensor()
{
    if(mProximityController != null)
    {
        mProximityController.unregisterListener();
    }
}

You will need these 2 files (find them in zip) for this code to work, import them in your project, change package name to match your project’s and use above code.

ProximitySensorAPI.zip

1) ProximitySensorController.java

2) ProximitySensorManager.java

Follow android developers guide for Proximity Sensor here : Position Sensors | Android Developers

Thanks,
Vipul

facebooktwittergoogle_plusredditpinterestlinkedinmailfacebooktwittergoogle_plusredditpinterestlinkedinmailby feather