Using Proximity Sensor in Android

Hello everyone!

The term proximity is basically the fact, condition or position of being near or close by in space. A proximity sensor is a device capable of detecting presence of nearby objects without any physical contact. Some of the Android phones in the market today come equipped with a proximity sensor.

Through this post, we will learn how to use a proximity sensor in Android. As mentioned in the developer's guide, 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).

Begin by creating a new Activity called ProximityTestActivity in any of your existing Android projects and add the following code!

ProximityTestActivity.java


import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class ProximityTestActivity extends Activity {
	/** Called when the activity is first created. */

	TextView txtProximitySensor, txtMaxProximity, txtReadingProximity;
	SensorManager mSensorManager;
	Sensor mProximitySensor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_proximity);
		
		txtProximitySensor = (TextView)findViewById(R.id.txtProximitySensor);
		txtMaxProximity = (TextView)findViewById(R.id.txtProximityMax);
		txtReadingProximity = (TextView)findViewById(R.id.txtProximityReading);

		mSensorManager = (SensorManager)getSystemService(
				Context.SENSOR_SERVICE);
		mProximitySensor = mSensorManager.getDefaultSensor(
				Sensor.TYPE_PROXIMITY);

		if (mProximitySensor == null){
			txtProximitySensor.setText("Proximity sensor is not present!"); 
		
		}else{
			txtProximitySensor.setText("Sensor present with name:" + " " +mProximitySensor.getName());
			txtMaxProximity.setText("Maximum Range: "
					+ String.valueOf(mProximitySensor.getMaximumRange()));
			mSensorManager.registerListener(proximitySensorEventListener,
					mProximitySensor,
					SensorManager.SENSOR_DELAY_NORMAL);
		}
	}

	SensorEventListener proximitySensorEventListener
	= new SensorEventListener(){
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			// TODO Auto-generated method stub
			if(event.sensor.getType()==Sensor.TYPE_PROXIMITY)
			{
				txtReadingProximity.setText("Proximity Sensor Reading:"
						+ String.valueOf(event.values[0]));
			}
		}
	};
}

activity_proximity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtProximitySensor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/txtProximityMax"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtProximitySensor"/>

    <TextView
        android:id="@+id/txtProximityReading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtProximityMax"/>

</RelativeLayout>

Save all changes. Make sure no errors are present. Run the application on an Android device and you should see the following output!

android_proximity_sensor_output_1

android_proximity_sensor_output_2

Reference: Position sensors