Registering an accelerometer listener with a custom rate
-
03-07-2021 - |
Question
private static int SENSOR_DELAY_SLOW = 5000000;
sensMngr = (SensorManager) getSystemService(SENSOR_SERVICE);
sensMngr.registerListener(this, sensMngr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SENSOR_DELAY_SLOW);
I want to capture the sensor's data every 5 seconds but it ignores my desired rate. The listener captures values even over 400 times per second.
Of course I can filter incoming data manually in the onSensorChanged(SensorEvent event) method, but my primary aim is to reduce the battery drain.
What am I doing wrong?
Thank you.
Solution
You want SensorManager.SENSOR_DELAY_UI and you're going to have to filter down from there. There are only 4 selectable speeds and you can't define your own. SENSOR_DELAY_UI tends to go a max of around 3 samples per second and since you are getting results when the sensor values change it can be a few seconds between samples. This will all vary from device to device.
There is no way to define a fixed rate for sensor value delivery in Android.