2016-05-22 2 views
0

J'ai cette application dans laquelle l'orientation de l'écran est fixé sur portrait et au lieu de faire une rotation en plein écran, et avoir à reconstruire l'activité, j'ai décidé de utilisez plutôt l'accéléromètre. Le code suivant fonctionne correctement sur tous les périphériques testés jusqu'à ce que je rencontre ce périphérique (One + 6.0), dans lequel je n'ai pas pu obtenir le champ géomagnétique pour calculer l'orientation.Android n'a pas pu obtenir Sensor Type Sensor.TYPE_MAGNETIC_FIELD sur certains appareils

Est-ce un problème avec le matériel? Ai-je manqué quelques permissions? Je l'ai examiné et je n'ai trouvé aucune documentation indiquant que je dois demander des autorisations pendant l'exécution pour un accéléromètre.

Voici le code du 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; 
         } 

        } 
       } 
      } 
     } 

Répondre

0

Vous n'avez pas besoin Magnetic sensor pour pitch et roll. Juste filtre passe-bas accelerometer pour obtenir la gravité.

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); 
} 
+0

Salut, Pouvez-vous expliquer comment obtenir mGravity et le gravityNorm que vous avez mentionné précédemment? Merci. – Minitour