I use the following (assuming to be quite standard) code to retrieve the orientation of a tablet device for augmented reality purpose (so the back-camera should point a valid direction).
private Sensor mAccelerometer;
private Sensor mMagnetometer;
private float[] mLastAccelerometer = new float[3];
private boolean mLastAccelerometerSet = false;
private float[] mLastMagnetometer = new float[3];
private boolean mLastMagnetometerSet = false;
private Sensor mAccelerometer;
private Sensor mMagnetometer;
private float[] mLastAccelerometer = new float[3];
private boolean mLastAccelerometerSet = false;
private float[] mLastMagnetometer = new float[3];
private boolean mLastMagnetometerSet = false;
private float[] inR = new float[9];
private float[] mR = new float[9];
private float[] mOrientation = new float[3];
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
...
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
...
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_FASTEST);
...
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
if (event.sensor == mAccelerometer) {
System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
mLastAccelerometerSet = true;
} else if (event.sensor == mMagnetometer) {
System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
mLastMagnetometerSet = true;
}
if (mLastAccelerometerSet && mLastMagnetometerSet && scene.camera() != null && mPlane1 != null) {
SensorManager.getRotationMatrix(inR, null, mLastAccelerometer, mLastMagnetometer);
SensorManager.remapCoordinateSystem(inR, SensorManager.AXIS_X, SensorManager.AXIS_Z, mR);
SensorManager.getOrientation(mR, mOrientation);
/* my code that handles mOrientation */
}
}
}
The problem is that this code works flawlessly on a Samsung Galaxy Tab 10.1 (P7500, first version). With the same code running on a Asus Transformer Pad Infinity (TF700), the first value mOrientation[0] only varies between +2.0 and +3.0 when rotating the tablet around myself (I would expect that it will go between 0 and 3.14).
Is there something wrong with the device or are there other variables that I overlooked?