title: Android Sensors: Compass
body: <p>I'm currently working on a little compass app. So far so good, I got the compass working. I'm using the <code>TYPE_ROTATION_VECTOR</code>, since the result is much more stable than using <code>TYPE_ACCELEROMETER</code> combined with <code>TYPE_MAGNETIC_FIELD</code>. That's my code to calculate the azimuth:</p>
  
  <pre><code>SensorManager.getRotationMatrixFromVector(rotationMatrix, sensorEvent.values );
  // original values are within [-180,180]
  azimuth = (float) (Math.toDegrees(SensorManager.getOrientation(rotationMatrix, orientation)[0]) + 360) % 360;
  </code></pre>
  
  <p>If the device is laying flat on the table, the results are correct. If I lift my phone, so it's standing (the screen is facing to me now), I get incorrect results. I guess that's because my axes changed. How can i properly detect and handle this? </p>