0

I'm trying to get the device orientation on a Galaxy Note 2. This is my code for the onSensorChanged() method but the RotationMatrix returns false constantly and hence the orientation is not calculated. I've spent several hours going through docs and googling for solutions but they all provide a similar implementation and I can't seem to understand what I'm doing wrong.

For test purposes, I've just output Azimut to the console.

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]);
        }
    }

This is the 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
ayushn21
  • 305
  • 2
  • 7

2 Answers2

1

Try adding these two permissions in the Manifest file:

android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION

Good luck programming!

Rohodude
  • 495
  • 2
  • 5
  • 18
  • Thanks for your answer! Sorry, but I probably wasn't too clear. I need the angle of orientation around the Z axis. Something like what you would use for a compass app. Is there a way to do this? – ayushn21 Dec 02 '13 at 01:06
  • Done already! The code works fine right up until the point where getRotationMatrix is called. It always returns false and hence the code does not progress from there. Is getRotationMatrixFromVector a valid substitute for that method ? – ayushn21 Dec 02 '13 at 01:17
  • @ayushn21 Can you put the LogCat in your question? It would help a lot. – Rohodude Dec 02 '13 at 01:18
  • Added LogCat as requested – ayushn21 Dec 02 '13 at 01:24
0
gravityMatrix = event.values.clone(); 

and similarly for magneticFieldMatrix

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54