5

我想获得手机的方向,并且我使用了这个代码,我发现很多人都在使用它。这是代码

    public void onSensorChanged(SensorEvent event) {
    //if the data sensor is unreliabel
    if(event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
        return;

    //gets the value
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        gravity = event.values.clone();
        break;

    case Sensor.TYPE_MAGNETIC_FIELD:
        geomag = event.values.clone();
        break;
    }
    getOrientation();
}

private void getOrientation(){

    //if gravity n geomag have value, find the rotation matrix
            if(gravity != null && geomag != null){

                //check the rotation matrix found
                boolean success = SensorManager.getRotationMatrix(inR, I, gravity, geomag);

                if(success){
                    SensorManager.getOrientation(inR, orientVals);
                    azimuth = Math.toDegrees(orientVals[0]);

                    TextView azi = (TextView) findViewById(R.id.textAzi);
                    azi.setText("azi : " + azimuth);

                }
                TextView cek = (TextView) findViewById(R.id.cek);
                cek.setText("rotation: "+success);
            }   

但是为什么 getrotationmatrix 总是返回 false 呢?哪里有问题?

4

3 回答 3

5

我再次尝试并获得解决方案,但我将代码更改为如下所示

    public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        for(int i=0; i<3; i++){
            accelValues[i] =  event.values[i];
        }
        if(compassValues[0] != 0)
            ready = true;

        break;

    case Sensor.TYPE_MAGNETIC_FIELD:
        for(int i=0; i<3; i++){
            compassValues[i] = event.values[i];
        }
        if(accelValues[2] != 0)
            ready = true;

        break;
    }

    if(!ready)
        return;

    boolean cek = SensorManager.getRotationMatrix(inR, inclineMatrix, accelValues, compassValues);

    if(cek){
        SensorManager.getOrientation(inR, prefValues);
        mInclination = SensorManager.getInclination(inclineMatrix);

        //display every 30th values
        if(counter++ % 30 == 0){
            //do your code, what you want to do to the result
            counter = 1;
        }
    }

一切正常

于 2013-01-08T21:12:46.540 回答
3

确保您已注册您的听众以收听加速度计、磁力场和陀螺仪(可选)。您可以在 onResume 方法中执行此操作。如果您忘记执行此操作,您将在调用 getRotationMatrix 时获得“假”返回值。

我希望这有帮助!

   protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

   mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

   // do more staff here..
   }


   protected void onResume() {
    super.onResume();

    mSensorManager.registerListener(
            this, 
            mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
             SensorManager.SENSOR_DELAY_NORMAL );
          mSensorManager.registerListener(
            this, 
            mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
            SensorManager.SENSOR_DELAY_NORMAL );
          mSensorManager.registerListener(
                this, 
                mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), 
                SensorManager.SENSOR_DELAY_NORMAL );


}

protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
}
于 2013-01-22T15:20:49.760 回答
0

问题在于这部分代码:

if(event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
    return;

对于加速度计 event.accuracy 返回 SensorManager.SENSOR_STATUS_UNRELIABLE 这就是为什么重力总是空的。

于 2017-10-10T10:17:35.727 回答