I want to get the current magnetic orientation regardless of the current screen orientation (landscape or portrait).
I found this example, but it\'s not orientation
Sensor.TYPE_ORIENTATION is depreciated and only good if the device is flat. When using Sensor.TYPE_ORIENTATION, the bearing (azimuth) is the direction where the device Y-axis points. So if the device is held vertical, the direction where the Y-axis points using as the bearing does not make sense. It only make sense to calculate the direction where the back camera points. To find this direction you have to use Sensor.TYPE_MAGNETIC_FIELD and Sensor.TYPE_GRAVITY or Sensor.TYPE_ACCELEROMETER. If using Sensor.TYPE_ACCELEROMETER, you have to filter the accelerometer values.
Using these sensors, you call getRotationMatrix and then remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR) before calling getOrientation. To get a stable direction you should keep a history of the direction and then calculate the average. For an implementation using TYPE_GRAVITY check Android getOrientation Azimuth gets polluted when phone is tilted