Android Wearable Compass无法在Android Nougat中使用

时间:2018-11-29 07:45:28

标签: android android-sensors watch compass wearables

Android手表上的Android佩戴式指南针仅显示视图,但在Android牛轧糖中指南针不动,认为该功能在Android Lollipop上非常完美。 该应用程序还可以在所有版本的Android模拟器上正常运行,但在具有Android Nougat的真实设备中,该应用程序无法正常工作。 我正在使用来自www.laipac.com的Look Watch 这是一款Android 7.0智能手表。

这是我的代码。

public class CompassActivity extends WearableActivity implements SensorEventListener {

private ImageView imageView;
float[] mGravity = new float[3];
float[] mGeomagnetic = new float[3];
float azimuth = 0f;
float currectAzimuth = 0f;
private SensorManager mSensorManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compass);
    setAmbientEnabled();
    imageView = findViewById(R.id.imageViewCompass);
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

}

@Override
protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    final float alpha = 0.97f;
    synchronized (this){
        if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
            mGravity[0] = alpha*mGravity[0]+(1-alpha)*sensorEvent.values[0];
            mGravity[1] = alpha*mGravity[1]+(1-alpha)*sensorEvent.values[1];
            mGravity[2] = alpha*mGravity[2]+(1-alpha)*sensorEvent.values[2];
        }

        if(sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
            mGeomagnetic[0] = alpha*mGeomagnetic[0]+(1-alpha)*sensorEvent.values[0];
            mGeomagnetic[1] = alpha*mGeomagnetic[1]+(1-alpha)*sensorEvent.values[1];
            mGeomagnetic[2] = alpha*mGeomagnetic[2]+(1-alpha)*sensorEvent.values[2];
        }

        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R,I,mGravity,mGeomagnetic);
        if(success){
            float oriention[] = new float[3];
            SensorManager.getOrientation(R, oriention);
            azimuth = (float) Math.toDegrees(oriention[0]);
            azimuth = (azimuth+360)%360;

            Animation anim = new RotateAnimation(-currectAzimuth, -azimuth, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            currectAzimuth = azimuth;
            anim.setDuration(500);
            anim.setRepeatCount(0);
            anim.setFillAfter(true);
            imageView.startAnimation(anim);
        }
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}
}

任何帮助将不胜感激

0 个答案:

没有答案