簡體   English   中英

Android在某些設備上無法獲取Sensor Type Sensor.TYPE_MAGNETIC_FIELD

[英]Android could not get Sensor Type Sensor.TYPE_MAGNETIC_FIELD on some devices

我有一個此應用,其中屏幕方向固定為縱向,而不是進行全屏旋轉,並且不得不重新構建活動,因此我決定使用加速度計。

下列代碼在我測試的所有設備上都可以正常工作,直到碰到這台設備(運行6.0的One +)為止,在該設備中,我無法獲取地磁場來計算方向。

這是硬件問題嗎? 我缺少一些權限嗎? 我已經研究過了,但沒有找到任何文檔說明我需要在運行時為加速度計請求權限。

這是onSensor的代碼:

    public void onSensorChanged(SensorEvent event) {

            boolean isOrientationEnabled;
            try {
                isOrientationEnabled = Settings.System.getInt(getContentResolver(),
                        Settings.System.ACCELEROMETER_ROTATION) == 1;
            } catch (Settings.SettingNotFoundException e) {
                isOrientationEnabled = false;
            }
            if (!isOrientationEnabled){
                if(this.Orientation!= ORIENTATION_PORTRAIT)rotateViews(ORIENTATION_PORTRAIT);
                this.Orientation = ORIENTATION_PORTRAIT;
                return;
            }

            if(allow_rotation) {

                if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
                    mGravity = event.values;
                if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
                    mGeomagnetic = event.values;
//mGeomagnetic is null
                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 orientation[] = new float[3];
                        SensorManager.getOrientation(R, orientation);
                        // orientation contains: azimut, pitch and roll
                        float pitch = (float) Math.toDegrees(orientation[1]);
                        if (pitch < -45 && pitch > -135) {
                            // if device is laid flat on a surface
                            if(this.Orientation!= ORIENTATION_PORTRAIT) rotateViews(ORIENTATION_PORTRAIT);
                            this.Orientation = ORIENTATION_PORTRAIT;
                            return;
                        }
                        float roll = (float) Math.abs(Math.toDegrees(orientation[2]));
                        if ((roll > 60 && roll < 135)) {
                            // The device is closer to landscape orientation. Enable fullscreen
                            int landscape_mode;//0 = right, 2 = left
                            if (Math.toDegrees(orientation[2]) > 0) landscape_mode = ORIENTATION_LANDSCAPE_RIGHT;
                            else landscape_mode = ORIENTATION_LANDSCAPE_LEFT;


                            if(this.Orientation!=landscape_mode) rotateViews(landscape_mode);
                            this.Orientation = landscape_mode;
                        } else if (roll < 45 && roll > 135) {
                            // The device is closer to portrait orientation. Disable fullscreen

                            if(this.Orientation!=1)rotateViews(ORIENTATION_PORTRAIT);
                            this.Orientation = ORIENTATION_PORTRAIT;
                        }

                    }
                }
            }
        }

您不需要Magnetic sensor進行pitchroll 只需低通濾波器accelerometer即可獲得重力。

private static final float ALPHA = 0.8;
private float[] mGravity;

public void onSensorChanged(SensorEvent event) {
    mGravity[0] = ALPHA * mGravity[0] + (1 - ALPHA) * event.values[0];
    mGravity[1] = ALPHA * mGravity[1] + (1 - ALPHA) * event.values[1];
    mGravity[2] = ALPHA * mGravity[2] + (1 - ALPHA) * event.values[2];

    double gravityNorm = Math.sqrt(mGravity[0] * mGravity[0] + mGravity[1] * mGravity[1] + mGravity[2] * mGravity[2]);

    pitch = (float) Math.asin(-mGravity[1] / gravityNorm);
    roll = (float) Math.atan2(-mGravity[0] / gravityNorm, mGravity[2] / gravityNorm);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM