The latest version of the SDK adds the phrase "type_orientation this constant is deprecated. Use Sensormanager.getorientation () instead. This means that the method has been canceled, and the developer uses sensormanager.getorientation () to get the original data.
In fact, the Android get direction is obtained by the magnetic field sensor and the accelerometer sensor, as for the specific algorithm SDK has been encapsulated. In other words, there are two ways to get the user direction now, one is the official recommendation, through the sensormanager.getorientation () to obtain, this method surface seems easy (that is because you have not seen his parameters.) But, in fact, two sensors are needed to do the work together, and the feature is more accurate. The second method is very simple, just like the previous article gets the acceleration, directly get the data on the three axes.
Well, from the difficult introduction, because after all, the first method will be an Android future choice, the second one does not know when it will become history.
Android gives us the direction data is a float type array that contains values in three directions
When your phone is placed horizontally, it defaults to a static state, with an xy angle of 0
Values[0] represents the z axis angle: direction angle, we usually judge the east is to see this data, after my experiment, found an interesting thing, that is to use the first way to get direction (magnetic field + acceleration) The data range is ( -180~180), that is, 0 means true North, 90 means east, 180/-180 is south, and 90 means due west. The second way (directly through the direction sensor) data range is (0~360) 360/0 means True North, 90 means east, 180 is south, 270 means due west.
VALUES[1] represents the angle of the x-axis: The pitch angle starts at the stationary state and flips back and forth
VALUES[2] represents the angle of the y-axis: The flip angle starts at rest and flips left and right
It is necessary to see a uniform way to get directions, because the algorithm that processes the data may be for the first acquisition, so the portability is not good when used in the second way.
Look at the following method
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- --------
public static float[] Getorientation (float[] R, float[] values)
SINCE:API Level 3
Computes the device ' s orientation based on the rotation matrix.
When it returns, the array values are filled with the result:
Values[0]: Azimuth, rotation around the Z axis.
VALUES[1]: Pitch, rotation around the X axis.
VALUES[2]: Roll, rotation around the Y axis.
The reference Coordinate-system used is different from the world Coordinate-system defined for the rotation matrix:
X is defined as the vector product y.z (It's tangential to the ground at the device's current location and roughly points West).
Y is tangential to the ground at the device's current location and points towards the magnetic North Pole.
Z points towards the center of the Earth and is perpendicular to the ground.
All three angles above is in radians and positive in the counter-clockwise direction.
Usually we don't need to get the return value of this function, which is populated with the data of the parameter r[] values[] and the latter is what we want.
So what does R mean? And how will it be acquired?
R[] is a rotation matrix used to hold the data of the magnetic field and acceleration, you can understand the raw direction of the data bar
R is obtained by the following static method, which is also used to populate r[]
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
Okay, here's the basic logic, and here's an example of a simple test direction, where you can listen to the user's direction at all times.
/*
* @author Octobershiner
* 2011 07 28
* SE. Hit
* An example that demonstrates the acquisition of directional data by a magnetic field and acceleration two sensors
* */
Package uni.sensor;
Import android.app.Activity;
Import Android.content.Context;
Import Android.hardware.Sensor;
Import android.hardware.SensorEvent;
Import Android.hardware.SensorEventListener;
Import Android.hardware.SensorManager;
Import Android.os.Bundle;
Import Android.util.Log;
public class Orientationactivity extends activity{
Private Sensormanager SM;
Requires two sensor
Private Sensor asensor;
Private Sensor msensor;
float[] accelerometervalues = new Float[3];
float[] magneticfieldvalues = new Float[3];
private static final String TAG = "sensor";
@Override
public void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
SM = (Sensormanager) getsystemservice (Context.sensor_service);
Asensor = Sm.getdefaultsensor (Sensor.type_accelerometer);
Msensor = Sm.getdefaultsensor (Sensor.type_magnetic_field);
Sm.registerlistener (MyListener, Asensor, sensormanager.sensor_delay_normal);
Sm.registerlistener (MyListener, msensor,sensormanager.sensor_delay_normal);
Update methods for displaying data
Calculateorientation ();
}
Again, note that when activity is paused, release
public void OnPause () {
Sm.unregisterlistener (MyListener);
Super.onpause ();
}
Final Sensoreventlistener MyListener = new Sensoreventlistener () {
public void onsensorchanged (Sensorevent sensorevent) {
if (sensorEvent.sensor.getType () = = Sensor.type_magnetic_field)
Magneticfieldvalues = sensorevent.values;
if (sensorEvent.sensor.getType () = = Sensor.type_accelerometer)
Accelerometervalues = sensorevent.values;
Calculateorientation ();
}
public void onaccuracychanged (sensor sensor, int accuracy) {}
};
private void Calculateorientation () {
Float[] values = new FLOAT[3];
float[] R = new FLOAT[9];
Sensormanager.getrotationmatrix (R, NULL, accelerometervalues, magneticfieldvalues);
Sensormanager.getorientation (R, values);
To go through a data format conversion, convert to degrees
Values[0] = (float) math.todegrees (values[0]);
LOG.I (TAG, values[0]+ "");
Values[1] = (float) math.todegrees (values[1]);
VALUES[2] = (float) math.todegrees (values[2]);
if (Values[0] >=-5 && Values[0] < 5) {
LOG.I (TAG, "true North");
}
else if (Values[0] >= 5 && values[0] < 85) {
LOG.I (TAG, "Tohoku");
}
else if (Values[0] >= && values[0] <=95) {
LOG.I (TAG, "Zhengdong");
}
else if (Values[0] >= && values[0] <175) {
LOG.I (TAG, "southeast");
}
else if ((Values[0] >= 175 && values[0] <= 180) | | (Values[0]) >= -180 && Values[0] <-175) {
LOG.I (TAG, "South");
}
else if (Values[0] >= -175 && values[0] <-95) {
LOG.I (TAG, "Southwest");
}
else if (Values[0] >= -95 && Values[0] <-85) {
LOG.I (TAG, "due West");
}
else if (Values[0] >= -85 && values[0] <-5) {
LOG.I (TAG, "Northwest");
}
}
}
Android Sensor Sensor Introduction (iii) Get user direction of movement, compass principle