我使用以下(假设是非常标准的)代码来检索平板电脑设备的方向以实现增强现实(因此后置摄像头应指向有效的方向)。
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 */
}
}
}
问题是此代码在三星Galaxy Tab 10.1 (P7500,第一版)上完美运行。使用相同的代码在华硕Transformer Pad Infinity(TF700)上运行时,第一个值mOrientation [0]仅在我自己旋转平板电脑时在+2.0和+3.0之间变化(我希望它会介于0和3.14之间。
设备有问题或是否有其他我忽略的变量?
答案 0 :(得分:0)
华硕TF700的硬件肯定存在一些错误。它可以是我们拥有的特定版本,也可以是设备的设计版本。使用其他Android设备,应用程序按预期工作。