Android的Sensor.TYPE_ORIENTATION完美的作品,我不能得到与加速度计和磁力

2019-09-20 12:53发布

我想使用这个代码非过时的版本,以获得方位。 但是我不能让它的工作顺利和完美的,因为如果我只是使用过时Sensor.TYPE_ORIENTATION。 我缺少的东西吗?

//旧

@Override
public void onSensorChanged(SensorEvent arg0) {
    m_lastYaw = arg0.values[0];
    invalidate();
}

//新

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE){
        return;
    }

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        System.arraycopy(event.values, 0, m_lastAccels, 0, 3);
    }
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        System.arraycopy(event.values, 0, m_lastMagFields, 0, 3);
    }

    if (SensorManager.getRotationMatrix(m_rotationMatrix, null, m_lastAccels, m_lastMagFields)) {
        SensorManager.getOrientation(m_rotationMatrix, m_orientation); 

        m_lastYaw = (float)Math.toDegrees(m_orientation[0]);
        invalidate();
    }
}

Answer 1:

I used the code that I posed here and it worked well only when the phone (Samsung Galaxy S3) layed flat. But it's a start: It doesn't work better than Sensor.TYPE_ORIENTATION nor worse.



文章来源: Android Sensor.TYPE_ORIENTATION works perfect and I can't get the same results with accelerometer and magnetometer