1

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

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

SENSOR_STATUS_ACCURACY_HIGH

我怎样才能实现这个价值?

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

4

2 回答 2

0

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

于 2011-10-17T23:28:34.017 回答
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);
}
于 2017-03-20T17:34:04.840 回答