title: Android magnetometer returning random values
body: <p>I am building an Android application which logs the degrees of the compass of the device into a file. There are two methods the get this degrees:</p>
  
  <p><strong>Method 1:</strong></p>
  
  <pre><code>SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  Sensor orientationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
  mSensorManager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);
  
  
  public void onSensorChanged(SensorEvent event) {
      float azimuthInDegrees = event.values[0]
  }
  </code></pre>
  
  <p><strong>Method 2:</strong></p>
  
  <pre><code>SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  Sensor accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  Sensor magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
  mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
  mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
  
  float[] mGravity;
  float[] mGeomagnetic;
  
  public void onSensorChanged(SensorEvent event) {
      if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
         mGravity = event.values;
      }
  
  
      if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
          mGeomagnetic = event.values;
      }
  
      if (mGravity != null &amp;&amp; mGeomagnetic != null) {
          float R[] = new float[9];
          float I[] = new float[9];
          boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
          if (success) {
              float orientation[] = new float[3];
              SensorManager.getOrientation(R, orientation);   
  
              float azimuthInDegress = ((float) Math.toDegrees(orientation[0]) + 360) % 360;
          }
      }
  }
  </code></pre>
  
  <p>I tried out both methods by placing my device in the North direction (which is around 360 degrees):</p>
  
  <p><strong>Method 1</strong> returns perfect results but unfortunately this method is deprecated:</p>
  
  <ul>
  <li>359.6567</li>
  <li>359.5034</li>
  <li>359.859</li>
  <li>359.76212</li>
  <li>359.8878</li>
  <li>359.87048</li>
  <li>359.8356</li>
  <li>359.80356</li>
  <li>359.81192</li>
  <li>359.7671</li>
  <li>359.84668</li>
  <li>359.88528</li>
  </ul>
  
  <p><strong>Method 2</strong> also returns good results but sometimes (randomly) it returns an incorrect degree:</p>
  
  <ul>
  <li>359.91495</li>
  <li>359.83652</li>
  <li><strong>263.67697</strong></li>
  <li>359.67993</li>
  <li>359.70038</li>
  <li>359.688</li>
  <li>359.71155</li>
  <li>359.70276</li>
  <li>359.6984</li>
  <li>359.6429</li>
  <li><strong>270.6323</strong></li>
  <li>359.62302</li>
  <li>359.49954</li>
  <li>359.44757</li>
  <li>359.47803</li>
  <li>359.4947</li>
  <li>359.39572</li>
  </ul>
  
  <p>As you can see, some incorrect degrees are randomly returned with the second method. The device is calibrated and I think that the problem is with the second method as the first method returns perfect results. Can you guys help me out?</p>