title: Logic for finding qibla direction using accelerometer and magnetic field sensors
body: <p>I am trying to implement the logic for finding the qibla direction.</p>
  <p>I used <code>Sensor.TYPE_ORIENTATION</code> and copied the code from <a href="https://stackoverflow.com/questions/41036933/universal-logic-for-finding-qibla-direction">here</a></p>
  <pre class="lang-java prettyprint-override"><code>mSensorManager =  (SensorManager) context.getSystemService(SENSOR_SERVICE);
  sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
  </code></pre>
  <p>but I found that the <code>Sensor.TYPE_ORIENTATION</code> is deprecated and instead, I used accelerometer and magnetic field sensors from <a href="https://stackoverflow.com/questions/15003787/how-can-i-replace-type-orientation-is-deprecated-for-android-4-0-3">here</a></p>
  <p>here is my code now</p>
  <pre><code>private void registerSensor() {
          mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
          accelerometer = mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
          magnetometer = mySensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
          mySensorManager.registerListener(mySensorEventListener, accelerometer, SensorManager.SENSOR_DELAY_UI);
          mySensorManager.registerListener(mySensorEventListener, magnetometer, SensorManager.SENSOR_DELAY_UI);
      }
  </code></pre>
  <pre><code>private SensorEventListener mySensorEventListener = new SensorEventListener() {
          float degree;
          float head;
          @Override
          public void onAccuracyChanged(Sensor sensor, int accuracy) {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void onSensorChanged(SensorEvent event) {
              // TODO Auto-generated method stub
              if (lastKnownLocation == null || image == null || arrow == null) {
                  return;
              }
  
              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);
                      degree = orientation[2]; // orientation contains: azimut, pitch and roll
                      head = orientation[0];
                  }
              }
  </code></pre>
  <p>I am trying to get the degree and head. what am I doing wrong here?</p>