简体   繁体   中英

Compass on a watch face on Android wear

I am trying to implement a compass in my watch face, but i have troubles from the very beginning.

 public class SensorActivity extends Activity implements SensorEventListener {
    private final SensorManager mSensorManager;
    private final Sensor mSensor;

    public SensorActivity() {
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }

    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

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

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent event) {
        if(event.sensor.getType() == mSensor.getType())
            float mag = event.values[];
    }
}

So this is my code, the thing is i don't know how to get from the magnetic sensor degrees so that i can use it in a matrix to rotate the compass png as it should a normal one.

public void onSensorChanged(SensorEvent event) {
        if(event.sensor.getType() == mSensor.getType())
            float mag = event.values[];
    }

i think here is my problem, there is 1,2 and 3 that i can write in the brackets from " event.values[], but it gives me an error, it says " Not a statement"

So how can i do so i can get from the needed sensor, a value in degrees to use in my matrix ?

Your mag variable should be an array, and you probably want to declare it out on scope, so that values persist and can be used somewhere else:

public class SensorActivity extends Activity implements SensorEventListener {
    private final SensorManager mSensorManager;
    private final Sensor mSensor;
    private float[] mag = new float[3];

    public SensorActivity() {
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }

    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

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

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent event) {
        if(event.sensor.getType() == mSensor.getType()) {
             mag = event.values;
        }
    }
}

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