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

后端 未结 2 1401
清歌不尽
清歌不尽 2021-01-24 21:44

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

相关标签:
2条回答
  • 2021-01-24 22:09

    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.

    0 讨论(0)
  • 2021-01-24 22:22

    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 讨论(0)
提交回复
热议问题