Direction sensor for Android sensor

Source: Internet
Author: User

In general, getting the location information of the phone in the Android system has the Type_orientation constant in the API, which can be obtained as the accelerometer sensor Sm.getdefaultsensor (sensor.type_ ORIENTATION); However, we do so in the latest version of the SDK will see this sentence: "Type_orientation this constant is deprecated. Use Sensormanager.getorientation () instead. "That way also expires, not recommended!" Google recommends that we use sensormanager.getorientation () in our applications to get raw data.

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

The first parameter is r[] is a rotation matrix, used to hold the magnetic field and acceleration of the data, can be understood as the function of the incoming value, through which this function to find the azimuth.

The second parameter is the output of the function, and he has the function to fill us in automatically, which is what we want.

Values[0]: direction angle, but with (magnetic field+acceleration) The range of data obtained is (-180~ the),in other words,0represents True North, -indicates the east,180/-180said South,-90represents due west. The data range directly through the direction sensor is (0~359)360/0represents True North, -indicates the east, thesaid South, therepresents due west.

Values[1] Pitch Tilt Angle that is, start from the stationary state, reverse the front and back, the top of the mobile phone up (0~-90), the phone tail upward lift (0~90)

values[2] Roll Rotation Angle that is, start from the static state, flip left and right, the left side of the phone lifted (0~90), the mobile phone to lift (0~-90)


Now the question is how this r[] gets, in fact he is obtained through the function Getrotationmatrix.

Look at the definition of Getrotationmatrix:

public static Boolean Getrotationmatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)

Explain the following parameters, the first one is the R array we need to populate, the size is 9

The second is a transformation matrix, which converts the magnetic field data into actual gravity coordinates, which can be set to NULL by default.

The third is an array of size 3 that represents the data obtained from the accelerometer sensor in onsensorchanged

The fourth is an array of size 3, representing the data obtained from the magnetic field sensor in onsensorchanged

Example code:

Package Com.example.testoritation;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;// Implement sensor Event monitoring: Sensoreventlistenerpublic class Mainactivity extends Activity implements Sensoreventlistener{private Sensormanager sensormanager;private sensor acc_sensor;private sensor mag_sensor;//Accelerometer data float accvalues[]=new float [3];//geomagnetic sensor data float magvalues[]=new float[3];//rotation matrix, data for storing magnetic fields and accelerations float r[]=new float[9];//analog Direction sensor data (raw data in radians) float Values[]=new Float[3]; TextView show_change=null; @Overrideprotected 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 the sensor for monitoring: Sensormanager.registerlistener (this, acc_sensor, sensormanager.sensor_delay_game); Sensormanager.registerlistener (this, mag_sensor,sensormanager.sensor_delay_game);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} callback method when sensor state changes @overridepublic void Onsensorchanged (Sensorevent event) {if (Event.sensor.getType () ==sensor.type_ ACCELEROMETER) {accvalues=event.values;} else if (Event.sensor.getType () ==sensor.type_magnetic_field) {magvalues=event.values;} /**public Static Boolean Getrotationmatrix (float[] r, float[] I, float[] gravity, float[] geomagnetic) * Fills the rotated array R * r: to fill the Rotating an array * I: converting magnetic field data into actual gravity coordinates can be set to NULL by default * Gravity: Accelerometer data * Geomagnetic: Geomagnetic sensor data */sensormanager.getrotationmatrix ( R, NULL, Accvalues, magvalues),/** * public static float[] Getorientation (float[] R, float[] values) * R: Rotating Array * Values: Analog direction sensor data */sensormanager.getorientation (R, values);//convert radians to angles output StringBuffer buff =new StringBuffer (); for (float value:values) {value= (float) math.todegrees (value); Buff.append (value+ "");} Show_change.settext (Buff.tostring ());} @Overridepublic void onaccuracychanged (sensor sensor, int accuracy) {}}





Direction sensor for Android sensor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.