问题
KitKat introduced sensor batching, but i cant reading data from sensors in batching mode.
My code:
public class MainActivity extends AppCompatActivity implements SensorEventListener{
private SensorManager senSensorManager;
private Sensor senAccelerometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Log.d("myApp", "x = " + sensorEvent.values[0]);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
protected void onPause() {
super.onPause();
senSensorManager.unregisterListener(this);
}
protected void onResume() {
super.onResume();
int maxReportLatencyUs = 1000000; //1sec
int sampleRate = 20; //20hz
boolean batchMode = senSensorManager.registerListener(this, senAccelerometer, Math.round(1000f/sampleRate)*1000,maxReportLatencyUs);
Log.d("myApp", "batchMode = " + batchMode + " getFifo = " + senAccelerometer.getFifoReservedEventCount());
}
}
Logs:
12-06 16:41:51.871 16530-16530/ai.myapplication D/myApp: batchMode = true getFifo = 10000
12-06 16:41:52.000 16530-16530/ai.myapplication D/myApp: x = 0.18681335
12-06 16:41:52.070 16530-16530/ai.myapplication D/myApp: x =1.0407257
12-06 16:41:52.141 16530-16530/ai.myapplication D/myApp: x = 1.3413391
12-06 16:41:52.176 16530-16530/ai.myapplication D/myApp: x = 0.85072327
12-06 16:41:52.212 16530-16530/ai.myapplication D/myApp: x =-0.5851135
12-06 16:41:52.282 16530-16530/ai.myapplication D/myApp: x = 0.73765564
12-06 16:41:52.317 16530-16530/ai.myapplication D/myApp: x = -1.6987457
12-06 16:41:52.387 16530-16530/ai.myapplication D/myApp: x =-0.16067505
12-06 16:41:52.422 16530-16530/ai.myapplication D/myApp: x = 0.14190674
12-06 16:41:52.493 16530-16530/ai.myapplication D/myApp: x = 0.26727295
12-06 16:41:52.528 16530-16530/ai.myapplication D/myApp: x =0.09738159
12-06 16:41:52.599 16530-16530/ai.myapplication D/myApp: x = 0.1519928
12-06 16:41:52.634 16530-16530/ai.myapplication D/myApp: x = 0.18717957
12-06 16:41:52.669 16530-16530/ai.myapplication D/myApp: x =0.15489197
12-06 16:41:52.739 16530-16530/ai.myapplication D/myApp: x = 0.13951111
12-06 16:41:52.776 16530-16530/ai.myapplication D/myApp: x = 0.1558075
12-06 16:41:52.846 16530-16530/ai.myapplication D/myApp: x =0.14822388
12-06 16:41:52.880 16530-16530/ai.myapplication D/myApp: x = 0.16821289
12-06 16:41:52.952 16530-16530/ai.myapplication D/myApp: x = 0.11381531
12-06 16:41:52.986 16530-16530/ai.myapplication D/myApp: x =0.1874237
I expect to receive 20 events at end of each seconds, not 1 event every 0.05 sec
according to sensor batching
When max_report_latency_ns>0, sensor events do not need to be reported as soon as they are detected. They can be temporarily stored in the hardware FIFO and reported in batches, as long as no event is delayed by more than max_report_latency_ns nanoseconds. That is, all events since the previous batch are recorded and returned at once. This reduces the amount of interrupts sent to the SoC and allows the SoC to switch to a lower power mode (idle) while the sensor is capturing and batching data.
Please help
Thanks
来源:https://stackoverflow.com/questions/40998355/android-how-i-receive-sensors-data-in-batching-mode