开发者

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

I'm making an application that works as a compass, though I want to calibrate the acceleromet开发者_运维问答er 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!


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜