Android using a hardware compass

I can not find much information on the Android hardware compass. I keep trying to adjust my criteria to get Android to use a hardware compass. Basically, my problem is that I want the bearing to be updated even when the device is stationary.

In my opinion, GPS will only update your bearing when you move. I would like the update to be updated regardless of whether you are moving or not. I tried to trick the device and thought it was moving, setting the speed at a constant 10, but the bearing always returns 0.0.

My testing device (currently) is the HTC Tattoo. He has a compass so no problem. So my question is: is there a way to get the bearing to update with a hardware compass or not? whether your moves or not?

+3
source share
1 answer

Have you seen this sample code ? Here a is SensorListenerused to request 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);

So this is deprecated code. SensorManager now has a method getOrientation()that you can use. This, however, requires a rotation matrix. See here for an example .

+2
source

Source: https://habr.com/ru/post/1748521/


All Articles