デバイスのコンパスの度数をファイルに記録する Android アプリケーションを作成しています。この度数を取得するには、次の 2 つの方法があります。
方法 1:
SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor orientationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
mSensorManager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);
public void onSensorChanged(SensorEvent event) {
float azimuthInDegrees = event.values[0]
}
方法 2:
SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sensor magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
float[] mGravity;
float[] mGeomagnetic;
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
mGravity = event.values.clone();
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mGeomagnetic = event.values.clone();
}
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
float azimuthInDegress = ((float) Math.toDegrees(orientation[0]) + 360) % 360;
}
}
}
デバイスを北方向に配置し、時計回りに 360 度サイクル全体を実行して、再び北方向に戻ることで、両方の方法を試しました。
方法 1は、ログに記録された次の JSON データを返します。
[
{
"start time":"25-03-2013 20:42:11.071",
"direction":"N",
"end time":"25-03-2013 20:42:14.711"
},
{
"start time":"25-03-2013 20:42:14.781",
"direction":"NE",
"end time":"25-03-2013 20:42:18.842"
},
{
"start time":"25-03-2013 20:42:18.912",
"direction":"E",
"end time":"25-03-2013 20:42:21.643"
},
{
"start time":"25-03-2013 20:42:21.712",
"direction":"SE",
"end time":"25-03-2013 20:42:25.072"
},
{
"start time":"25-03-2013 20:42:25.142",
"direction":"S",
"end time":"25-03-2013 20:42:27.524"
},
{
"start time":"25-03-2013 20:42:27.593",
"direction":"SW",
"end time":"25-03-2013 20:42:30.113"
},
{
"start time":"25-03-2013 20:42:30.184",
"direction":"W",
"end time":"25-03-2013 20:42:32.773"
},
{
"start time":"25-03-2013 20:42:32.843",
"direction":"NW",
"end time":"25-03-2013 20:42:34.943"
},
{
"start time":"25-03-2013 20:42:35.013",
"direction":"N",
"end time":"25-03-2013 20:42:37.394"
}
]
方法 2では、次の JSON データがログに記録されます。
[
{
"start time":"25-03-2013 20:36:07.337",
"direction":"N",
"end time":"25-03-2013 20:36:09.728"
},
{
"start time":"25-03-2013 20:36:09.741",
"direction":"NE",
"end time":"25-03-2013 20:36:13.832"
},
{
"start time":"25-03-2013 20:36:13.832",
"direction":"E",
"end time":"25-03-2013 20:36:16.689"
},
{
"start time":"25-03-2013 20:36:16.754",
"direction":"SE",
"end time":"25-03-2013 20:36:16.754"
},
{
"start time":"25-03-2013 20:36:16.819",
"direction":"E",
"end time":"25-03-2013 20:36:16.819"
},
{
"start time":"25-03-2013 20:36:16.819",
"direction":"SE",
"end time":"25-03-2013 20:36:16.819"
},
{
"start time":"25-03-2013 20:36:16.884",
"direction":"E",
"end time":"25-03-2013 20:36:17.014"
},
{
"start time":"25-03-2013 20:36:17.014",
"direction":"SE",
"end time":"25-03-2013 20:36:19.546"
},
{
"start time":"25-03-2013 20:36:19.546",
"direction":"S",
"end time":"25-03-2013 20:36:22.338"
},
{
"start time":"25-03-2013 20:36:22.338",
"direction":"SW",
"end time":"25-03-2013 20:36:25.260"
},
{
"start time":"25-03-2013 20:36:25.324",
"direction":"W",
"end time":"25-03-2013 20:36:25.324"
},
{
"start time":"25-03-2013 20:36:25.324",
"direction":"SW",
"end time":"25-03-2013 20:36:25.390"
},
{
"start time":"25-03-2013 20:36:25.390",
"direction":"W",
"end time":"25-03-2013 20:36:27.987"
},
{
"start time":"25-03-2013 20:36:28.051",
"direction":"NW",
"end time":"25-03-2013 20:36:28.128"
},
{
"start time":"25-03-2013 20:36:28.181",
"direction":"W",
"end time":"25-03-2013 20:36:28.181"
},
{
"start time":"25-03-2013 20:36:28.181",
"direction":"NW",
"end time":"25-03-2013 20:36:28.181"
},
{
"start time":"25-03-2013 20:36:28.246",
"direction":"W",
"end time":"25-03-2013 20:36:28.246"
},
{
"start time":"25-03-2013 20:36:28.246",
"direction":"NW",
"end time":"25-03-2013 20:36:30.974"
},
{
"start time":"25-03-2013 20:36:31.038",
"direction":"N",
"end time":"25-03-2013 20:36:36.233"
}
]
ご覧のとおり、2 番目の方法の結果は最初の方法ほどスムーズではありませんが、北から北へ 1 回転しました。私は最初の方法を使用することを好みますが、問題はそれが推奨されていないことです。一方、2 番目の方法では、滑らかなデータはログに記録されません。皆さんの意見では、私は何をすべきですか?