I'm currently try to make an Android demo to test light level at a sampling rate about 50Hz. However, I could only get onSensorChanged
callback 2-3 times a second. My phone is xiaomi mi5s(lineage os 14.1 installed), Android 7.1.
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mLight;
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
@Override
public final void onSensorChanged(SensorEvent event) {
// The light sensor returns a single value.
// Many sensors return 3 values, one for each axis.
float lux = event.values[0];
Log.i("zz", lux + "");
// Do something with this sensor value.
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
You can use SENSOR_DELAY_FASTEST instead of normal. That would make it as fast as the hardware gives reliable readings for. But its entirely possible that the light sensor doesn't work at 50 hz. The major use cases for it don't require that.