views:

334

answers:

1

Hi Guys,

I can't find much information out there on Android's hardware compass. I keep trying to setup my criteria to force Android to use the hardware compass. Basically my problem is I want the bearing to update even while the device is stationary. From my understanding the GPS will only update your bearing when your moving. I'd like bearing to update regardless if your moving or not. I've tried tricking the device in to thinking it's moving by setting the speed at a constant 10 but the bearing returns 0.0 always. My device for testing (currently) is a HTC tattoo. It has a compass so that's not the problem. So my question is, is there a way to get the bearing to update using hardware compass or not? regardless if your moving or not?

Thanks,

+2  A: 

Have you seen this example code? Here a SensorListener is used to query the orientation:

mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mListener = new SensorListener() {
    public void onSensorChanged(int sensor, float[] values) {
        if (Config.LOGD) Log.d(TAG, "sensorChanged (" + values[0] + ", " + values[1] + ", " + values[2] + ")");
        mValues = values;
        if (mView != null) {
            mView.invalidate();
        }
    }
mSensorManager.registerListener(mListener, 
                    SensorManager.SENSOR_ORIENTATION,
                    SensorManager.SENSOR_DELAY_GAME);

Ok, so this is deprecated code. Now the SensorManager has a getOrientation() method that can be used. This does need a rotation matrix though. See here for an example.

Marc
That's using depreciated API's though, is there no other way?
Ulkmun
Sorry, I've been away from Android for a couple of months. I see that there is a `SensorManager.getOrientation(float[])` method now.
Marc
This is what I was looking for! Thanks! :)
Ulkmun