Application of Android Direction Sensor

  • 2021-10-16 04:55:49
  • OfStack

Use SensorManager. getOrientation () in your application to get the raw data.


public static float[] getOrientation (float[] R, float[] values)

The first parameter is R, which is used to store the data of magnetic field and acceleration, and obtains the azimuth angle through this function.

The second parameter is the function output, and the data is automatically filled.

values [0]: Direction angle, but the data range obtained by (magnetic field + acceleration) is (-180 ~ 180), that is, 0 means due north, 90 means due east, 180/-180 means due south, and-90 means due west. The data range directly passing through the direction sensor is (0 ~ 359) 360/0 for due north, 90 for due east, 180 for due south, and 270 for due west. values [1]: The tilt angle of pitch starts from the static state, turns back and forth, and the top of the mobile phone is lifted up (0 ~-90), and the tail of the mobile phone is lifted up (0 ~ 90) values [2]: The rotation angle of roll starts from the static state, turns left and right, and the left side of the mobile phone is lifted (0 ~ 90), and the right side of the mobile phone is lifted (0 ~-90)

Get R through function getRotationMatrix


public static boolean getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)

Register monitor


sensorManager.registerListener(this, acc_sensor, SensorManager.SENSOR_DELAY_GAME); 
sensorManager.registerListener(this, mag_sensor,SensorManager.SENSOR_DELAY_GAME); 

Main code


import android.app.Activity; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.TextView; 
 
public class MainActivity extends Activity implements SensorEventListener{ 
 
  private SensorManager sensorManager; 
  private Sensor acc_sensor; 
  private Sensor mag_sensor; 
  // Acceleration sensor data  
  float accValues[] = new float[3]; 
  // Geomagnetic sensor data  
  float magValues[] = new float[3]; 
  // Rotation matrix, which is used to store data of magnetic field and acceleration  
  float r[] = new float[9]; 
  // Analog direction sensor data (original data is radian)  
  float values[] = new float[3]; 
  TextView showTV = null; 
  
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    show_change=(TextView) findViewById(R.id.show_change); 
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
    acc_sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
    mag_sensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); 
    //  Register for listening:  
    sensorManager.registerListener(this, acc_sensor, SensorManager.SENSOR_DELAY_GAME); 
    sensorManager.registerListener(this, mag_sensor,SensorManager.SENSOR_DELAY_GAME); 
  } 
 
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
  }
   
  //  Callback method  
  @Override 
  public void onSensorChanged(SensorEvent event) { 
    if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){ 
      accValues = event.values.clone();
    } 
    else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){ 
      magValues = event.values.clone();
    }
    
    /**
     * r Rotation array to fill  
     * I:  Convert magnetic field data into actual gravity coordinates ,1 By default, it can be set to null 
     * gravity:  Acceleration sensor data  
     * geomagnetic Geomagnetic sensor data  
     */ 
    SensorManager.getRotationMatrix(r, null, accValues, magValues);
     
    /** 
     * R Rotate Array  
     * values Analog Direction Sensor Data  
     */ 
    SensorManager.getOrientation(r, values); 
     
    // Output after converting radian into angle  
    StringBuffer buff = new StringBuffer(); 
    for(float value : values){ 
      value=(float) Math.toDegrees(value); 
      buff.append(value + " "); 
    } 
    
    showTV.setText(buff.toString());   
  } 
   
  @Override 
  public void onAccuracyChanged(Sensor sensor, int accuracy) { 
  } 
}

Related articles: