简体   繁体   中英

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.

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?

这可能是因为存在一个固定的SensorEvents池,如果您直接使用它们中的数据,它可以从您的下方更改,但通过克隆它您可以随时使用该数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM