Android上的设备方向

时间:2013-12-02 00:55:43

标签: java android orientation accelerometer magnetometer

我正在尝试在Galaxy Note 2上获取设备方向。这是我的onSensorChanged()方法的代码,但RotationMatrix不断返回false,因此不计算方向。我花了几个小时浏览文档和谷歌搜索解决方案,但它们都提供了类似的实现,我似乎无法理解我做错了什么。

出于测试目的,我只是将Azimut输出到控制台。

private float[] gravityMatrix;
private float[] magneticFieldMatrix;
@Override
public void onSensorChanged(SensorEvent event) 
{
    if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
    {
        gravityMatrix = event.values;
    }
    if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
    {
        magneticFieldMatrix = event.values;
    }
    if(gravityMatrix != null && magneticFieldMatrix != null)
    {
        float[] rotationMatrix = new float[9];
        float[] inclinationMatrix = new float[9];
        boolean success = SensorManager.getRotationMatrix(rotationMatrix, inclinationMatrix, gravityMatrix, magneticFieldMatrix);
        System.out.println(success);
        if(success)
        {
            float orientation[] = new float[3];
            SensorManager.getOrientation(rotationMatrix, orientation);
            System.out.println("Azimut: " + orientation[0]);
        }
    }

这是LogCat:

12-02 01:20:45.885: D/GestureDetector(18770): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
12-02 01:20:46.040: D/SensorManager(18770): registerListener :: handle = 0  name= LSM330DLC Acceleration Sensor delay= 200000 Listener= com.Ayush.Finch.Misson_Finch.ActivityControlPanel@42bfefd0
12-02 01:20:46.045: D/SensorManager(18770): registerListener :: handle = 1  name= AK8963C Magnetic field Sensor delay= 200000 Listener= com.Ayush.Finch.Misson_Finch.ActivityControlPanel@42bfefd0
12-02 01:20:46.050: D/SensorManager(18770): onAccuracyChanged :: accuracy = 3
12-02 01:20:46.050: I/System.out(18770): false
12-02 01:20:46.195: I/System.out(18770): false
12-02 01:20:46.245: I/System.out(18770): false
12-02 01:20:46.375: I/System.out(18770): false
12-02 01:20:46.445: I/System.out(18770): false
12-02 01:20:46.555: I/System.out(18770): false
12-02 01:20:46.645: I/System.out(18770): false
12-02 01:20:46.735: I/System.out(18770): false
12-02 01:20:46.845: I/System.out(18770): false
12-02 01:20:46.915: I/System.out(18770): false
12-02 01:20:47.045: I/System.out(18770): false
12-02 01:20:47.095: I/System.out(18770): false
12-02 01:20:47.245: I/System.out(18770): false
12-02 01:20:47.275: I/System.out(18770): false
12-02 01:20:47.445: I/System.out(18770): false
12-02 01:20:47.455: I/System.out(18770): false
12-02 01:20:47.635: I/System.out(18770): false
12-02 01:20:47.645: I/System.out(18770): false
12-02 01:20:47.815: I/System.out(18770): false
12-02 01:20:47.845: I/System.out(18770): false
12-02 01:20:47.995: I/System.out(18770): false
12-02 01:20:48.045: I/System.out(18770): false
12-02 01:20:48.175: I/System.out(18770): false
12-02 01:20:48.245: I/System.out(18770): false
12-02 01:20:48.355: I/System.out(18770): false
12-02 01:20:48.445: I/System.out(18770): false
12-02 01:20:48.535: I/System.out(18770): false
12-02 01:20:48.650: I/System.out(18770): false
12-02 01:20:48.715: I/System.out(18770): false
12-02 01:20:48.850: I/System.out(18770): false
12-02 01:20:48.895: I/System.out(18770): false
12-02 01:20:49.045: I/System.out(18770): false
12-02 01:20:49.075: I/System.out(18770): false
12-02 01:20:49.245: I/System.out(18770): false
12-02 01:20:49.255: I/System.out(18770): false
12-02 01:20:49.415: D/GestureDetector(18770): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
12-02 01:20:49.415: D/SensorManager(18770): unregisterListener::  Listener= com.Ayush.Finch.Misson_Finch.ActivityControlPanel@42bfefd0
12-02 01:20:49.420: D/Sensors(18770): Remain listener = Sending .. normal delay 200ms
12-02 01:20:49.420: I/Sensors(18770): sendDelay --- 200000000
12-02 01:20:49.420: D/SensorManager(18770): JNI - sendDelay
12-02 01:20:49.420: I/SensorManager(18770): Set normal delay = true
12-02 01:20:49.420: D/Sensors(18770): Remain listener = Sending .. normal delay 200ms
12-02 01:20:49.420: I/Sensors(18770): sendDelay --- 200000000
12-02 01:20:49.420: D/SensorManager(18770): JNI - sendDelay
12-02 01:20:49.420: I/SensorManager(18770): Set normal delay = true

2 个答案:

答案 0 :(得分:1)

尝试在清单文件中添加这两个权限:

android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION

好运编程!

答案 1 :(得分:0)

gravityMatrix = event.values.clone(); 

,类似于magnetFieldMatrix