Using a magnetic sensor

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

The SensorListener class is a class of my own SensorEventListener interface. It has the OnSensorChanged method, I get the values ​​from the sensor, and I show them. The problem is that I only get values ​​of 1.0 and 0. And they are rarely updated (I set the counter to OnSensorChanged calls to find out how often the update happens). Changing the time to SENSOR_DELAY_NORMAL does not improve anything.

To check if the problem was related to the magnetic sensor, I added, in the same way, a listener to the accelerometer sensor. The result is very confusing: now the magnetic sensor generates updates, but not an accelerometer. And if I delete the accelerometer sensor event receiver, I still get magnetic sensor events that are missing before adding the accelerometer sensor event receiver. (???????????)

Any idea on what's wrong in my code?

+4
source share
1 answer

Just put this together and it works fine on my phone (HTC Desire, 2.2), please check if you have a problem with the phone with this ... in this 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 text elements, valMag_X, valMag_Y, valMag_Z.

+6
source

All Articles