如何检查传感器事件侦听活动中 OnAccuracyChanged 方法的准确度值?

发布于 2024-11-08 06:14:41 字数 216 浏览 0 评论 0原文

我正在制作一个用作指南针的应用程序,但我想校准加速度计和磁场传感器以使其更加高效。

从 API 中我了解到这是可能的,并且您可以检查一些值,例如:

SENSOR_STATUS_ACCURACY_HIGH

我怎样才能实现这个值?

我在OnAccuracyChanged里面思考,但不知道怎么做!

I'm making an application that works as a compass, though I want to calibrate the accelerometer and the magneticfield sensors to make it more efficient.

From the API I understand that this is possible, and that there are some values that you can check, for example:

SENSOR_STATUS_ACCURACY_HIGH

How can I achieve this value?

I was thinking inside OnAccuracyChanged, but don't know how!

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

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

发布评论

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

评论(2

烈酒灼喉 2024-11-15 06:14:41

当传感器开始以不同的精度报告时,系统会调用侦听器的 OnAccuracyChanged 方法 - 这是您从传感器收到的信息,因此您无法设置它。 SensorManager API 提供了一些枚举,可让您检查传感器报告的准确性。

The OnAccuracyChanged method of your listener is invoked by system, when a sensor begins to report with different accuracy - this is something you receive from the sensor, so you can not set it. SensorManager API has got some enumerations to let you check what accuracy is a sensor reporting at.

花伊自在美 2024-11-15 06:14:41

下面的代码

派生了一个实现 SensorEventListener 的类。

// Initialise the sensors
Sensor m_sensorAccelerometer = m_oSM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sensor m_sensorMagnetometer = m_oSM.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
Sensor m_sensorGyroscope = m_oSM.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
Sensor m_sensorGravity = m_oSM.getDefaultSensor(Sensor.TYPE_GRAVITY);

注册传感器监听器。

 SensorManager oSM = (SensorManager) m_Context.getSystemService(Context.SENSOR_SERVICE);
 oSM.registerListener(this, m_sensorAccelerometer, SensorManager.SENSOR_DELAY_GAME);
 oSM.registerListener(this, m_sensorMagnetometer,    SensorManager.SENSOR_DELAY_GAME);
 oSM.registerListener(this, m_sensorGyroscope, SensorManager.SENSOR_DELAY_GAME);
 oSM.registerListener(this, m_sensorGravity, SensorManager.SENSOR_DELAY_GAME);

 // Create a runable for a minute So that **onSensorChanged(SensorEvent event)** Method gets triggered
 Handler handler = new Handler();
 handler.postDelayed(new Runnable() {
     public void run() {
         LogUtils.getInstance().LOGR("TestSensors::postDelayed()");
         finish();
     }
 }, 1000);


// Override the method
public void onSensorChanged(SensorEvent event) {
    try {
        LogUtils.getInstance().LOGR("SensorTest::onSensorChanged=>" + event.accuracy);
        switch (event.sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
                // Read the event.accuracy;
                break;
            case Sensor.TYPE_GRAVITY:
                // Read the event.accuracy;                    
                break;
            case Sensor.TYPE_MAGNETIC_FIELD:
                // Read the event.accuracy;
                break;
            case Sensor.TYPE_GYROSCOPE:
                // Read the event.accuracy;
                break;
            default:
                break;
        }
    } catch (Exception e) {
        throw e;
    }
}

// unregister the Listeners
private void finish() {
    m_oSM.unregisterListener(this, m_sensorAccelerometer);
    m_oSM.unregisterListener(this, m_sensorMagnetometer);
    m_oSM.unregisterListener(this, m_sensorGyroscope);
    m_oSM.unregisterListener(this, m_sensorGravity);
}

Here is the code

Derived a class that implements SensorEventListener.

// Initialise the sensors
Sensor m_sensorAccelerometer = m_oSM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sensor m_sensorMagnetometer = m_oSM.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
Sensor m_sensorGyroscope = m_oSM.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
Sensor m_sensorGravity = m_oSM.getDefaultSensor(Sensor.TYPE_GRAVITY);

Register the sensors Listener.

 SensorManager oSM = (SensorManager) m_Context.getSystemService(Context.SENSOR_SERVICE);
 oSM.registerListener(this, m_sensorAccelerometer, SensorManager.SENSOR_DELAY_GAME);
 oSM.registerListener(this, m_sensorMagnetometer,    SensorManager.SENSOR_DELAY_GAME);
 oSM.registerListener(this, m_sensorGyroscope, SensorManager.SENSOR_DELAY_GAME);
 oSM.registerListener(this, m_sensorGravity, SensorManager.SENSOR_DELAY_GAME);

 // Create a runable for a minute So that **onSensorChanged(SensorEvent event)** Method gets triggered
 Handler handler = new Handler();
 handler.postDelayed(new Runnable() {
     public void run() {
         LogUtils.getInstance().LOGR("TestSensors::postDelayed()");
         finish();
     }
 }, 1000);


// Override the method
public void onSensorChanged(SensorEvent event) {
    try {
        LogUtils.getInstance().LOGR("SensorTest::onSensorChanged=>" + event.accuracy);
        switch (event.sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
                // Read the event.accuracy;
                break;
            case Sensor.TYPE_GRAVITY:
                // Read the event.accuracy;                    
                break;
            case Sensor.TYPE_MAGNETIC_FIELD:
                // Read the event.accuracy;
                break;
            case Sensor.TYPE_GYROSCOPE:
                // Read the event.accuracy;
                break;
            default:
                break;
        }
    } catch (Exception e) {
        throw e;
    }
}

// unregister the Listeners
private void finish() {
    m_oSM.unregisterListener(this, m_sensorAccelerometer);
    m_oSM.unregisterListener(this, m_sensorMagnetometer);
    m_oSM.unregisterListener(this, m_sensorGyroscope);
    m_oSM.unregisterListener(this, m_sensorGravity);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文