Compass on a watch face on Android wear

495 views Asked by At

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 ?

1

There are 1 answers

0
GarciadelCastillo On

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