I did it as follows:
1 - Get acceleration and magnetic values using acceleration and magnetic lifts.
2 - Convert these values to orientation:
private float[] rMatrix = new float[9]; private float[] outR = new float[9]; SensorManager.getRotationMatrix(rMatrix, null, accelerometerValues, magneticValues); SensorManager.remapCoordinateSystem(rMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, outR); SensorManager.getOrientation(outR, orientationValues);
Here, the Values accelerometer and magnetValues float [3] the arrays that you initialize according to the listeners
3 - valuesValues [0] and valuesValues [1] - horizontal and vertical angles.
source share