Using magnetic sensor

Zelig picture Zelig · Jun 13, 2011 · Viewed 8.6k times · Source

I want to get the three coordinates values of the magnetic field measured by the sensor of my phone. For this, I get a handle to the SensorManager by using sm=(SensorManager)getApplicationContext().getSystemService(Context.SENSOR_SERVICE), then get the sensor with cm=sm.getDefaultSensor(SensorManager.SENSOR_MAGNETIC_FIELD). I then register a SensorEventListener to the SensorManager with sm.registerListener(new SensorListener(),cm,SensorManager.SENSOR_DELAY_UI).

The classSensorListener is a class of my own implementing the SensorEventListener interface. In it's OnSensorChanged method, I get the values from the sensor and I display them. The problem is that I only get the values 1,0 and 0. And they are rarely updated (I have put a counter on the onSensorChanged calls to see how often the update takes place). Changing the time to SENSOR_DELAY_NORMAL doesnot improve anything.

To check if the problem was related to the magnetic sensor, I have added, in the same way, a listener to an accelerometer sensor. The result is very confusing : now, the magnetic sensor generates updates, but not the accelerometer one. And if I remove the accelerometer sensor event listener, I still receive the magnetic sensor events which where missing before adding the accelerometer sensor event listener.(???????????)

Any idea about what is wrong in my code?

Answer

Anindo Ghosh picture Anindo Ghosh · Jun 30, 2011

Just put this together and it works fine on my handset (HTC Desire, 2.2), please check if you face an issue on your phone with this... in which case there may be a problem with your device.

package com.SmartPhoneGizmos.examples.MagSensor;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class MagSensorActivity extends Activity  implements SensorEventListener {

    private TextView magneticX;
    private TextView magneticY;
    private TextView magneticZ;
    private SensorManager sensorManager = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        // Capture magnetic sensor related view elements
        magneticX = (TextView) findViewById(R.id.valMag_X);
        magneticY = (TextView) findViewById(R.id.valMag_Y);
        magneticZ = (TextView) findViewById(R.id.valMag_Z);

        // Register magnetic sensor
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onPause();
    }

    @Override
    protected void onStop() {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Register magnetic sensor
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Ignoring this for now

    }

    public void onSensorChanged(SensorEvent sensorEvent) {
        synchronized (this) {
            if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                magneticX.setText( Float.toString( sensorEvent.values[0]));
                magneticY.setText( Float.toString( sensorEvent.values[1]));
                magneticZ.setText( Float.toString( sensorEvent.values[2]));
            }
        }

    }
}

Your layout file will need three TextViews, valMag_X, valMag_Y, valMag_Z.