我需要识别用户如何握住他的设备。所以我试着以这种方式使用耦合加速度计和磁力计:
public void onSensorChanged(SensorEvent event) {
switch(event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
mGravity = event.values.clone();
Log.d("Sensor","ACCEL");
break;
case Sensor.TYPE_MAGNETIC_FIELD:
Log.d("Sensor","MAGNET");
mMagnetic = event.values.clone();
break;
default:
Log.d("Sensor","default");
}
if(mGravity != null && mMagnetic != null) {
//float[] temp = new float[9];
float[] R = new float[9];
//Load rotation matrix into R
SensorManager.getRotationMatrix(R, null,
mGravity, mMagnetic);
//Return the orientation values
float[] values = new float[3];
SensorManager.getOrientation(R, values);
//Convert to degrees
for (int i=0; i < values.length; i++) {
Double degrees = (values[i] * 180) / Math.PI;
values[i] = degrees.floatValue();
}
}
if (mValuesOrientation[0]!=0 || mValuesOrientation[1]!=0 || mValuesOrientation[2]!=0 ){
logOrientation();
}
}
};
但我有一些问题:
我该如何解决?