简体   繁体   English

使用磁传感器

[英]Using magnetic sensor

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) .为此,我使用sm=(SensorManager)getApplicationContext().getSystemService(Context.SENSOR_SERVICE)获取SensorManager的句柄,然后使用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) .然后,我使用sm.registerListener(new SensorListener(),cm,SensorManager.SENSOR_DELAY_UI)SensorEventListener注册到SensorManager

The class SensorListener is a class of my own implementing the SensorEventListener interface. class SensorListener是我自己实现SensorEventListener接口的 class。 In it's OnSensorChanged method, I get the values from the sensor and I display them.在它的OnSensorChanged方法中,我从传感器获取值并显示它们。 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).问题是我只得到值 1,0 和 0。而且它们很少更新(我在onSensorChanged调用上放置了一个计数器,以查看更新发生的频率)。 Changing the time to SENSOR_DELAY_NORMAL doesnot improve anything.将时间更改为SENSOR_DELAY_NORMAL不会改善任何问题。

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?关于我的代码有什么问题的任何想法?

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.只需将它放在一起,它就可以在我的手机(HTC Desire,2.2)上正常工作,请检查您的手机是否遇到此问题......在这种情况下,您的设备可能有问题。

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.您的布局文件将需要三个 TextView,valMag_X、valMag_Y、valMag_Z。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM