Search code examples
androidandroid-sensors

Get current SensorEvent value


I am using Sensor.TYPE_STEP_COUNTER. As far as I know, the android SensorEvent maintains a value since reboot. I can use the following codes to get the value once the onSensorChanged method is called.

Like:

public void onSensorChanged(SensorEvent event) {
    System.out.println(event.values[0] );
}

However, how can I get the current value without using such a method? I try to use :

SensorEvent event = new SensorEvent();
float value = event.value[0]; 

This won't work. Is there a similar way to get the current Sensor value?


Solution

  • The sensor information is only available as SensorEvent in the method onSensorChanged. The sensor can not be polled like you try to do.

    What you can do is to store the float[] event.values during the onSensorChanged method in a class member variable. This way you can retrieve it in the method where you want to use your event.values.