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

为Android应用程序创建方向传感器,android,orientation,sensors,android-sensors,Android,Orientation,Sensors,Android Sensors,我正在尝试为android应用程序制作一个方向传感器,但我的代码似乎仍然不起作用。我在android应用程序网站上寻求帮助,但仍然没有得到任何结果。我的代码没有产生错误,只是没有显示/收集结果 if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { //Orientation sensor if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)

我正在尝试为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 );
            }
        }