SoFunction
Updated on 2025-04-11

How to use Android direction sensor

Use() in the application to get the raw data.

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

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

The second parameter is 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 north, 90 means east, 180/-180 means south, and -90 means west. The data range of the direct-pass direction sensor is (0~359) 360/0 means north, 90 means east, 180 means south, and 270 means west.
  • values[1]: pitch The inclination angle starts from the stationary state, flips forward and backward, lifts the top of the phone upward (0~-90), and lifts the tail of the phone upward (0~90)
  • values[2]: roll rotation angle starts from the stationary state, flips left and right, lifts up on the left side of the phone (0~90), lifts up on the right side of the phone (0~-90)

Get R via the function getRotationMatrix

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

Register to monitor

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

Main code

import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
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]; 
  //The rotation matrix is ​​used to store the data of the magnetic field and acceleration  float r[] = new float[9]; 
  //Simulate the data of the direction sensor (the original data is radians)  float values[] = new float[3]; 
  TextView showTV = null; 
  
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(.activity_main); 
    show_change=(TextView) findViewById(.show_change); 
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
    acc_sensor = (Sensor.TYPE_ACCELEROMETER); 
    mag_sensor = (Sensor.TYPE_MAGNETIC_FIELD); 
    // Register to listen:    (this, acc_sensor, SensorManager.SENSOR_DELAY_GAME); 
    (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(, menu); 
    return true; 
  }
   
  // Callback method  @Override 
  public void onSensorChanged(SensorEvent event) { 
    if(() == Sensor.TYPE_ACCELEROMETER){ 
      accValues = ();
    } 
    else if(() == Sensor.TYPE_MAGNETIC_FIELD){ 
      magValues = ();
    }
    
    /**
      * r: The rotation array to be filled
      * I: Convert magnetic field data into actual gravity coordinates, which can generally be set to null by default
      * gravity: Acceleration sensor data
      * geomagnetic: geomagnetic sensor data
      */ 
    (r, null, accValues, magValues);
     
    /**
      * R: Rotate array
      * values: simulate the data of the direction sensor
      */ 
    (r, values); 
     
    //Open the output after converting radians to angles    StringBuffer buff = new StringBuffer(); 
    for(float value : values){ 
      value=(float) (value); 
      (value + " "); 
    } 
    
    (());   
  } 
   
  @Override 
  public void onAccuracyChanged(Sensor sensor, int accuracy) { 
  } 
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.