Android, Using the hardware compass

a 夏天 提交于 2019-12-22 07:58:40

问题


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 you're 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?


回答1:


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.



来源:https://stackoverflow.com/questions/2974185/android-using-the-hardware-compass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!