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

时间:2011-05-17 23:16:13

标签: android android-sensors

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

从API我明白这是可能的,并且您可以检查一些值,例如:

SENSOR_STATUS_ACCURACY_HIGH

我怎样才能达到这个价值?

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这是代码

派生了一个实现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);
}