0

I want to compute the compass' direction, so I use the following code:

    SensorManager mManager = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
    Sensor accelerometer = mManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    Sensor magnetometer = mManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    mManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
    private float mR[] = new float[9];
    private float mI[] = new float[9];
    float mOrientation[] = new float[3];

    ....

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 && mGeomagnetic != null) {
        boolean success = SensorManager.getRotationMatrix(mR, mI, mGravity, mGeomagnetic);
        if (success) {
            SensorManager.getOrientation(mR, mOrientation);
            int azimut = (int) (mOrientation[0]*180/3.14159f); // This is the data
        }
    }
}

It usually works fine. It does not work when there is an interference with the magnetic field - for example under my car radio. In that case - I get a wrong constant reading.

how can I detect magnetic interference?

DuduArbel
  • 911
  • 1
  • 9
  • 22

0 Answers0