Android的磁力返回随机值 [英] Android magnetometer returning random values

查看:225
本文介绍了Android的磁力返回随机值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个Android应用程序,它会记录度的设备的罗盘到文件中。有两种方法得到这个度:

I am building an Android application which logs the degrees of the compass of the device into a file. There are two methods the get this degrees:

方法1:

SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor orientationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
mSensorManager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);


public void onSensorChanged(SensorEvent event) {
    float azimuthInDegrees = event.values[0]
}

方法2:

SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sensor magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);

float[] mGravity;
float[] mGeomagnetic;

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

            float azimuthInDegress = ((float) Math.toDegrees(orientation[0]) + 360) % 360;
        }
    }
}

我放置我的设备在北方方向(即360度)尝试了两种方法

方法1 返回完美的效果但遗憾的是这种方法去precated:

Method 1 returns perfect results but unfortunately this method is deprecated:


  • 359.6567

  • 359.5034

  • 359.859

  • 359.76212

  • 359.8878

  • 359.87048

  • 359.8356

  • 359.80356

  • 359.81192

  • 359.7671

  • 359.84668

  • 359.88528

方法2 也返回了良好的效果,但有时(随机),它会返回一个不正确度:

Method 2 also returns good results but sometimes (randomly) it returns an incorrect degree:


  • 359.91495

  • 359.83652

  • 263.67697

  • 359.67993

  • 359.70038

  • 359.688

  • 359.71155

  • 359.70276

  • 359.6984

  • 359.6429

  • 270.6323

  • 359.62302

  • 359.49954

  • 359.44757

  • 359.47803

  • 359.4947

  • 359.39572

正如你所看到的,一些不正确的度是随机的第二种方法返回。该设备校准,我认为这个问题是第二个方法,第一个方法返回完美的效果。你们能帮助我吗?

As you can see, some incorrect degrees are randomly returned with the second method. The device is calibrated and I think that the problem is with the second method as the first method returns perfect results. Can you guys help me out?

推荐答案

这个问题在的assigment mGravity mGeomagnetic 应该是 event.values​​.clone()。 mGravity有一流的范围,但通过使用mGravity = event.values​​,你有方法范围的地址赋给它的值的值。所以一旦onSensorChanged被再次调用,它是磁类型,mGravity现在指向不再存在,因此可以具有任何值的变量。

The problem is in the assigment of mGravity and mGeomagnetic it should be event.values.clone(). mGravity has class scope but, by using mGravity = event.values, you assign its value to a value in an address that has method scope. So as soon as onSensorChanged is called again and it is magnetic type, the mGravity is now pointing to a variable which no longer exists and thus can have any value.

这篇关于Android的磁力返回随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆