Following herom's recommendations using the link: http://android-coding.blogspot.co.at/2012/03/create-our-android-compass.html
I expanded my class to implement the sensor: extends Activity implements SensorEventListener
And implemented as proposed, but changed it to take into account the orientation of the screen.
Here is the code I went with:
@Override public void onSensorChanged(SensorEvent event) { switch(event.sensor.getType()){ case Sensor.TYPE_ACCELEROMETER: for(int i =0; i < 3; i++){ valuesAccelerometer[i] = event.values[i]; } break; case Sensor.TYPE_MAGNETIC_FIELD: for(int i =0; i < 3; i++){ valuesMagneticField[i] = event.values[i]; } break; } boolean success = SensorManager.getRotationMatrix( matrixR, matrixI, valuesAccelerometer, valuesMagneticField); if(success){ SensorManager.getOrientation(matrixR, matrixValues); double azimuth = Math.toDegrees(matrixValues[0]);
source share