5

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?

dfeuer
  • 48,079
  • 5
  • 63
  • 167
Zelig
  • 157
  • 3
  • 12

1 Answers1

7

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.

Anindo Ghosh
  • 313
  • 4
  • 11
  • Well, the code you have given me works perfectly. In fact, since a few days, mine was working fine as well for the magnetic sensor, as I mentioned in my last paragraph. But, when I try to modify either my code or yours to retrieve the values from the accelerometer sensors, I still get the magnetics field values. Any idea which could explain this strange behaviour? – Zelig Jul 02 '11 at 08:46
  • It's always nice to accept answers, keeps the person giving responses interested. Difficult to guess at the answer to your extended question without seeing your code, so pasting your changes here would help. – Anindo Ghosh Jul 07 '11 at 13:00
  • The change made is very simple : changing all the "Sensor.TYPE_MAGNETIC_FIELD" to "Sensor.TYPE_ACCELEROMETER" – Zelig Jul 08 '11 at 10:34
  • Perhaps it would be possible to examine your code, if you would post the entire code as you have changed it. – Anindo Ghosh Jul 13 '11 at 07:40