Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current value of Android's proximity sensor?

Is it possible to get the current value of the Proximity Sensor in Android?

I know that I can use SensorManager and Sensor and register a state changed listener, but I have no need to be notified of every state change, so it would be highly inefficient since this code is being run in a service. Also, my code won't know the value until a state change has occurred (what if the value hasn't changed...how do I know what it is? Instead, rather than registering a listener, I just want to say:

proximitySensor.getCurrentDistance();

Is this possible?

Thanks

like image 946
Nullqwerty Avatar asked Feb 09 '16 21:02

Nullqwerty


People also ask

What is the maximum value of the light sensor?

Light sensor value range: 0~1000, exposed under sunshine (> 500), evening (0 ~ 100), lighting (100 to 500).

How can I get sensor data in Android?

You can access sensors available on the device and acquire raw sensor data by using the Android sensor framework. The sensor framework provides several classes and interfaces that help you perform a wide variety of sensor-related tasks.

How do you calibrate a proximity sensor?

Use an App to Re-Calibrate Your Device's Proximity Sensor Just like resetting your phone, recalibrating your sensors can help them run properly. To recalibrate your proximity sensor, use the Proximity Sensor Reset/Fix app, which is free from the Play Store.


2 Answers

After looking through the documentation, it looks like you can get the distance in centimeters by subscribing to the SensorEvent and looking at the data being passed back.

There is a good example of starting to use the Proximity sensor here: Android Proximity Sensor Example

After reading a bit further into the Android docs, it looks like the array values[0] returns a value in centimeters. Note, look at the docs, some sensors only return binary values, meaning that the device is either near or far.

like image 128
user3924722 Avatar answered Sep 18 '22 17:09

user3924722


To get access the device sensor using SensorManager, you have to call getSystemService(SENSOR_SERVICE) . Here is the example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".SensorActivity" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/far" />

</RelativeLayout>

Here is the java class:

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ImageView;

public class SensorActivity extends Activity implements SensorEventListener {
 private SensorManager mSensorManager;
 private Sensor mSensor;
 ImageView iv;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.sensor_screen);
  mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
  iv = (ImageView) findViewById(R.id.imageView1);
 }

 protected void onResume() {
  super.onResume();
  mSensorManager.registerListener(this, mSensor,
    SensorManager.SENSOR_DELAY_NORMAL);
 }

 protected void onPause() {
  super.onPause();
  mSensorManager.unregisterListener(this);
 }

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

 public void onSensorChanged(SensorEvent event) {
  if (event.values[0] == 0) {
   iv.setImageResource(R.drawable.near);
  } else {
   iv.setImageResource(R.drawable.far);
  }
 }
}
like image 41
Md. Sajedul Karim Avatar answered Sep 22 '22 17:09

Md. Sajedul Karim