Search code examples
javaandroidandroid-sensors

android OnSensorChanged() slow response


I have code implemented like this:

    //register sensor in OnResume
    mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
    mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);

    public void onSensorChanged(SensorEvent event) {

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        mGravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        mGeomagnetic = event.values;

    if (mGravity != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);

        if (success) {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);

            azimuth = (int)( Math.toDegrees( orientation[0] ) + 0.5 );    // orientation contains: azimuth, pitch and roll
            pitch = (int)( Math.toDegrees( orientation[1] ) + 0.5 );
            roll = (int)( Math.toDegrees( orientation[2] ) + 0.5 );

            // output azimuth, pitch and roll
        }
    }
}

The above code works fine in Galaxy Nexus cell, but it has problem in nexus tablet (the updating of azimuth, pitch and roll are not well responding, sometimes take 5 to 8 secs).

I have checked that the calling of OnSensorChanged() is working well, but the "if(success)" test is not always success which gets this problem.

I tested it by output the boolean variable "success":

  • In Galaxy Nexus, the rate of false and true is about 1:1.
  • In Nexus tablet, the rate of false and true varies a lot, can be high as >20 : 1.

Any help is much appreciated.


Solution

  • The problem lies with the following code:

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) mGravity = event.values; if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) mGeomagnetic = event.values; 
    

    it should be:

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) mGravity = event.values.clone(); 
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) mGeomagnetic = event.values.clone(); 
    

    Otherwise the "getRotationMatrix" method will frequently return "false". But I am not sure why?