在用户手中识别设备位置

时间:2015-06-25 14:47:46

标签: android accelerometer device-orientation magnetometer

我需要识别用户如何握住他的设备。所以我试着以这种方式使用耦合加速度计和磁力计:

public void onSensorChanged(SensorEvent event) {

        switch(event.sensor.getType()) {

            case Sensor.TYPE_ACCELEROMETER:
                mGravity = event.values.clone();
                Log.d("Sensor","ACCEL");
                break;
            case Sensor.TYPE_MAGNETIC_FIELD:
                Log.d("Sensor","MAGNET");
                mMagnetic = event.values.clone();
                break;
            default:
                Log.d("Sensor","default");

        }
        if(mGravity != null && mMagnetic != null) {
            //float[] temp = new float[9];
            float[] R = new float[9];
            //Load rotation matrix into R
            SensorManager.getRotationMatrix(R, null,
                    mGravity, mMagnetic);

            //Return the orientation values
            float[] values = new float[3];
            SensorManager.getOrientation(R, values);


            //Convert to degrees
            for (int i=0; i < values.length; i++) {
                Double degrees = (values[i] * 180) / Math.PI;
                values[i] = degrees.floatValue();
            }

        }
        if (mValuesOrientation[0]!=0 || mValuesOrientation[1]!=0 || mValuesOrientation[2]!=0 ){
            logOrientation();
        }
    }

    };

但我有一些问题:

  • 在相同的位置,我会获得类似的价值。但价值观是如此不同。

我该如何解决?

0 个答案:

没有答案