为Android应用程序创建方向传感器

时间:2015-11-17 17:58:17

标签: android orientation sensor android-sensors

我正在尝试为Android应用程序制作方向传感器,但我的代码似乎仍然不起作用。我在Android应用程序网站上寻求帮助,但仍无法获得任何结果。我的代码不会产生错误,它只是不显示/收集结果。

if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        //Orientation sensor
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
            mGravity = event.values;
        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
            mGeomagnetic = event.values;
        if (mGravity != null && mGeomagnetic != null) {
            float R[] = new float[9];
            float I[] = new float[9];
            boolean success = SensorManager.getRotationMatrix(R, I,    mGravity, mGeomagnetic);
            if (success) {
                float orientation2[] = new float[3];
                SensorManager.getOrientation(R, orientation2);

                String orientationText = "Orientation: \n -Z: " + df.format(orientation2[0]) +"\n-X: " + df.format(orientation2[1]) + "\nY: " + df.format(orientation2[2]);
                orientation.setText(orientationText);

                Log.d(TAG, "Gyroscope:" + orientation2[0] + "," + orientation2[1] + "," + orientation2[2]);

                MagnetometerData mData = new MagnetometerData( orientation2[0], orientation2[1], orientation2[2]) ;
                instance.addMagnetometerData( mData );
            }
        }

0 个答案:

没有答案
相关问题