Reputation: 653
I'm working on a project where among other things, I'm trying to obtain the accelerometer data without the gravity components. However, I don't seem to be able to get updates from the virtual sensors: "linear acceleration" and "gravity". Here's the code:
public class SensorModule implements SensorEventListener{
(...)
public SensorModule(Context context){
this.context = context;
sensorManager = (SensorManager) context.getSystemService(context.SENSOR_SERVICE);
sensorAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorMagneticField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorLinearAccel = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
sensorGravity = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {}
@Override
public void onSensorChanged(SensorEvent event) {
switch(event.sensor.getType()){
case Sensor.TYPE_ACCELEROMETER:
((SensorActivity)context).accelX.setText("" + event.values[0]);
((SensorActivity)context).accelY.setText("" + event.values[1]);
((SensorActivity)context).accelZ.setText("" + event.values[2]);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
((SensorActivity)context).magneX.setText("" + event.values[0]);
((SensorActivity)context).magneY.setText("" + event.values[1]);
((SensorActivity)context).magneZ.setText("" + event.values[2]);
break;
case Sensor.TYPE_LINEAR_ACCELERATION:
((SensorActivity)context).linearX.setText("" + event.values[0]);
((SensorActivity)context).linearY.setText("" + event.values[1]);
((SensorActivity)context).linearZ.setText("" + event.values[2]);
break;
case Sensor.TYPE_GRAVITY:
((SensorActivity)context).gravityX.setText("" + event.values[0]);
((SensorActivity)context).gravityY.setText("" + event.values[1]);
((SensorActivity)context).gravityZ.setText("" + event.values[2]);
}
}
public void registerListeners(){
sensorManager.registerListener(this, sensorAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, sensorMagneticField, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, sensorLinearAccel, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, sensorGravity, SensorManager.SENSOR_DELAY_NORMAL);
}
public void unregisterListeners(){
sensorManager.unregisterListener(this, sensorAccelerometer);
sensorManager.unregisterListener(this, sensorMagneticField);
sensorManager.unregisterListener(this, sensorLinearAccel);
sensorManager.unregisterListener(this, sensorGravity);
}
I register the listeners onResume() and unregister onPause() in the activity. I can get updates from the accelerometer and magnetometer just fine, but the TYPE_LINEAR_ACCELERATION and TYPE_GRAVITY are not producing any updates. I know my device has these virtual sensors from getSensorList(Sensor.TYPE_ALL). Also when I call getDefaultSensor(...) it returns non null for every one of these 4 sensors. I toasted each case inside the onSensorChanged() method individually, but the linear and gravity ones never show up, which means these updates are not being produced. I've also tried a larger delay (SENSOR_DELAY_UI) to give it more time, but still no dice. I'm testing on a Nexus 5 running Lollipop 5.0.1 (API 21). Anyone got a clue?
Upvotes: 0
Views: 1357
Reputation: 653
OK, by applying the first rule of engineering and general common sense the problem was solved :) Just turned it off and on again, and now it automagically works. This is a bit troubling though, because it means I have no way of knowing if the sensors are working properly other than using a timeout thread to verify periodic updates.
Upvotes: 2