How to check accuracy values on OnAccuracyChanged method in sensor event listening Activity?

有些话、适合烂在心里 提交于 2019-12-20 06:15:41

问题


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!


回答1:


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.




回答2:


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);
}


来源:https://stackoverflow.com/questions/6038232/how-to-check-accuracy-values-on-onaccuracychanged-method-in-sensor-event-listeni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!