Search code examples
androidandroid-sensors

Android how to set sensor delay?


For my project i've created a class that uses the Sensors of the users device to detect north. I only need to get an update every second, and hopefully by making the delay longer save some battery, but I cant get the delay working. What i have:

mAcceleroSensor = mSensorManagerAccelero.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
mMagneticSensor = mSensorManagerMagnetic.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// Tried to set normal values like SensorManager.SENSOR_DELAY_NORMAL
// and numbers like 100 - 100.000. 1.0000.0000 
// (without the dots only here to make it readable here).
mSensorManagerAccelero.registerListener(this, mAcceleroSensor, 50000000);
mSensorManagerMagnetic.registerListener(this, mMagneticSensor, 50000000);

Everything works except the delay it almost looks like its updated live. I have used code similair to https://stackoverflow.com/a/23060896/1667868 with some minour changes.

Note: the minimum api version I use is 14


Solution

  • The documentation for SensorManager.registerListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs):

    The events will be delivered to the provided SensorEventListener as soon as they are available. To reduce the power consumption, applications can use registerListener(SensorEventListener, Sensor, int, int) instead and specify a positive non-zero maximum reporting latency.

    Whilst the sensor's readings are sampled at the given interval, the reporting interval in that method is not specified causing the callback to be called rapidly as soon as a reading is available. Use the alternative method mentioned in the documentation as such:

     mSensorManagerAccelero.registerListener (this, mAcceleroSensor, 1000000, 1000000)
    

    to get it to report at about once at second (you can manually adjust the sampling rate as needed). This would also help with power usage.

    Note the documentation claims the final argument maxReportLatencyUs is the:

    Maximum time in microseconds that events can be delayed before being reported to the application.

    meaning that it could potentially still report faster than 1 second. If you want user's to see consistent smooth 1 second updates, consider saving results to a averaging buffer and updating the UI once per second.