I need to get orientation values and I follow the common approach:
@Override
public final void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()){
case Sensor.TYPE_ACCELEROMETER:
mGravity = event.values;
break;
case Sensor.TYPE_MAGNETIC_FIELD:
mGeomagnetic = event.values;
break;
}
if (mGravity == mGeomagnetic){
Log.w("Sensor", "Strange things happen!");
}
}
public float[] getOrientation(){
float orientation[] = new float[3];
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
if (SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic)) {
SensorManager.getOrientation(R, orientation);
return orientation;
} else {
Log.w("Sensor", "getRotationMatrix() failed");
}
}
return new float[]{0,0,0};
}
As you see, I've checked if two values stored separately equals. And I was forced to do this because Strange things really happen!
How could it be and how to avoid this? The check of event.sensor.getType() is sometimes incorrect. The logic does not work, what am I missing???
Here's some more intel. Switch statement works well, but the variables are mixed anyway.
Could it be that event.values contains old data??
UPD: If I understand correctly, the variables link to the same position in memory... What's wrong in here?
Try copying the SensorEvent values
field instead of keeping a reference.