使用磁传感器

发布于 2024-11-15 07:01:53 字数 833 浏览 3 评论 0原文

我想获取手机传感器测量的磁场的三个坐标值。为此,我使用 sm=(SensorManager)getApplicationContext().getSystemService(Context.SENSOR_SERVICE) 获取 SensorManager 的句柄,然后使用 获取传感器cm=sm.getDefaultSensor(SensorManager.SENSOR_MAGNETIC_FIELD)。然后,我使用 sm.registerListener(new SensorListener(),cm,SensorManager.SENSOR_DELAY_UI)SensorEventListener 注册到 SensorManager

SensorListener 类是我自己实现 SensorEventListener 接口的类。在它的 OnSensorChanged 方法中,我从传感器获取值并显示它们。问题是我只得到值 1,0 和 0。而且它们很少更新(我在 onSensorChanged 调用上放置了一个计数器来查看更新发生的频率)。将时间更改为 SENSOR_DELAY_NORMAL 不会改善任何情况。

为了检查问题是否与磁传感器有关,我以同样的方式添加了加速度计传感器的侦听器。结果非常令人困惑:现在,磁传感器生成更新,但加速度计不生成更新。如果我删除加速度计传感器事件侦听器,我仍然会收到在添加加速度计传感器事件侦听器之前丢失的磁传感器事件。(????????????)

关于我的代码中有什么问题的任何想法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苄①跕圉湢 2024-11-22 07:01:53

只需将其放在一起,它就可以在我的手机(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]));
            }
        }

    }
}

您的布局文件将需要三个 TextView:valMag_X、valMag_Y、valMag_Z。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文