Android development: Android device sensor Development Summary, android Sensor
In this article, I will share with you how to develop Android device sensors.
Please indicate the author xiong_it and link: http://blog.csdn.net/xiong_it/article/details/45917009
Android sensor development process
Public class sens?ti=extends Activity implements SensorEventListener {private SensorManager mSensorManager; private Sensor mSensor; @ Override public final void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // Step 1: Get the SensorManager Instance Object mSensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); // Step 2: obtain the desired sensor object through the SensorManager instance object: The parameter determines which sensor to obtain mSensor = mSensorManager. getdefasensensor (Sensor. TYPE_LIGHT);} // Step 4: the two methods that must be overwritten: onAccuracyChanged, onSensorChanged/*** callback interface for Sensor accuracy change */@ Override public final void onAccuracyChanged (sensor Sensor, int accuracy) {// TODO does some operations when sensor accuracy changes, callback interface when accuracy is the current sensor precision}/*** sensor event value is changed: the frequency of this method is related to the frequency of Sensor Registration */@ Override public final void onSensorChanged (SensorEvent event) {// Most sensors return three axis directions x, y, the event value of x. The meaning of the value varies depending on the sensor float x = event. values [0]; float y = event. values [1]; float z = event. values [2]; // do some operations using the obtained three float Sensor values}/*** Step 3: register the sensor when getting the focus and enable this class to implement the SensorEventListener interface */@ Override protected void onResume () {super. onResume ();/** first parameter: Instance Object of the SensorEventListener interface * second parameter: sensor instance to be registered * third parameter: Sensor obtains sensor event value frequency: * SensorManager. SENSOR_DELAY_FASTEST = 0: The Update Interval corresponding to 0 microseconds. The fastest value is 1 microsecond = 1% 1000000 seconds * SensorManager. SENSOR_DELAY_GAME = 1: corresponds to an update interval of 20000 microseconds. * SensorManager is often used in games. SENSOR_DELAY_UI = 2: Update Interval corresponding to 60000 microseconds * SensorManager. SENSOR_DELAY_NORMAL = 3: Update Interval corresponding to 200000 microseconds * when you type a custom int value x: Update Interval corresponding to x microseconds **/mSensorManager. registerListener (this, mSensor, SensorManager. SENSOR_DELAY_NORMAL);}/*** Step 5: unregister the sensor when the focus is lost */@ Override protected void onPause () {super. onPause (); mSensorManager. unregisterListener (this );}}
To sum up, there are roughly five steps:
Step 1: Get the SensorManager object
Step 2: Obtain the Sensor Object
Step 3: register the Sensor Object
Step 4: override the onAccuracyChanged and onSensorChanged Methods
Step 5: unregister the Sensor ObjectBasic types of sensors in Android devices
Use SensorManager to obtain the sensor:
MSensor = mSensorManager. getdefasensensor (int TYPE); // TYPE is the static final value defined in Sensor.
Refer to some TYPE values defined in Sensor. java below:
/*** Get the constant of the accelerometer */public static final int TYPE_ACCELEROMETER = 1;/*** get the constant of the magnetic sensor */public static final int TYPE_FIELD = 2; /*** get the constant of the direction sensor, out of date */@ Deprecated public static final int TYPE_ORIENTATION = 3; /*** get the constant of the gyroscope sensor */public static final int TYPE_GYROSCOPE = 4;/*** get the constant of the light sensor */public static final int TYPE_LIGHT = 5; /*** get the constant of the pressure sensor */public static final int TYPE_PRESSURE = 6; @ Deprecated public static final int TYPE_TEMPERATURE = 7; // get the temperature sensor, expired/*** get the constant */public static final int TYPE_PROXIMITY = 8;/*** get the constant */public static final int TYPE_GRAVITY = 9; /*** get the constant */public static final int TYPE_LINEAR_ACCELERATION = 10;/*** get the constant */public static final int TYPE_ROTATION_VECTOR = 11; /*** get the constant of the relative humidity sensor */public static final int TYPE_RELATIVE_HUMIDITY = 12;/*** get the constant of the temperature sensor: Disable public static final int TYPE_TEMPERATURE = 7; */public static final int TYPE_AMBIENT_TEMPERATURE = 13;
Interpretation of sensor event values in Android devices
The acceleration sensor, also known as G-sensor, returns the acceleration values of the x, y, and z axes.
This value contains the influence of gravity, measured in m/s ^ 2.
Place the mobile phone on the desktop. The default X axis is 0, the default Y axis is 0, and the default Z axis is 9.81.
Place the phone down on the desktop, and the Z axis is-9.81.
Tilt the phone to the left, and the X axis is positive.
Tilt the phone to the right, and the X axis is negative.
Tilt the phone up. the Y axis is negative.
Tilt the phone down, and the Y axis is positive.
The magnetic sensor is referred to as M-sensor and returns environmental magnetic field data of x, y, and z axes.
The unit of this value is micro-Tesla, expressed in uT.
The unit can also be Gaussian, 1 Tesla = 10000 Gauss.
Generally, there is no independent magnetic sensor on the hardware, and the magnetic data is provided by the E-compass sensor ).
The Electronic Compass sensor also provides the following direction sensor data.
The direction sensor (O-sensor) returns the angle data of three axes. The unit of the direction data is angle.
To obtain accurate angle data, E-compass needs to obtain G-sensor data,
The O-sensor data is calculated and produced. Otherwise, only the horizontal angle can be obtained.
The direction sensor provides three data types: azimuth, pitch, and roll.
Azimuth: returns the angle between the magnetic pole and the y-axis at the normal time, ranging from 0 ° to 360 °.
0 ° = North, 90 ° = east, 180 ° = South, 270 ° = West.
Pitch: the angle between the X axis and the horizontal plane. The range is-180 ° to 180 °.
When the Z axis rotates towards the Y axis, the angle is positive.
Roll: the angle between the Y axis and the horizontal plane. The range is-90 ° to 90 ° for historical reasons.
When the X axis moves toward the Z axis, the angle is positive.
The Electronic Compass requires calibration before obtaining the correct data. Generally, the 8-character calibration method is used.
The 8-character calibration method requires the user to perform 8-character shaking in the air using the equipment to be calibrated,
In principle, try to direct the device's normal direction to all eight quadrants of the space as much as possible.
The gyroscope sensor, called Gyro-sensor, returns the angular velocity data of x, y, and z axes.
The unit of the angular velocity is radians/second.
According to the Nexus S mobile phone test:
Rotate horizontally counterclockwise, And the Z axis is positive.
Rotate horizontally counterclockwise, And the Z axis is negative.
Rotate to the left, and the Y axis is negative.
Rotate to the right, and the Y axis is positive.
Rotate upwards. The X axis is negative.
Rotate downward. the X axis is positive.
The light sensor detects the real-time light intensity. The unit of light intensity is lux, and its physical meaning is the luminous flux that shines on the area.
The light sensor is mainly used for the LCD automatic brightness function of the Android system.
The brightness of the LCD can be adjusted in real time based on the sampled intensity value.
The pressure sensor of the pressure sensor returns the current pressure, in the unit of baipascal hectopascal (hPa ).
The temperature sensor returns the current temperature.
A distance sensor, also known as a proximity sensor, detects the distance between an object and a mobile phone. The unit is centimeter.
Some proximity sensors can only return two statuses: far and near,
Therefore, close to the sensor returns the maximum distance to the far state, less than the maximum distance to return the near state.
The proximity sensor can be used to automatically turn off the LCD screen when answering calls to save power.
Some chips integrate functions of proximity sensors and light sensors.
Gravity Sensors are referred to as GV-sensor, which output gravity data.
On the earth, the gravity value is 9.8, in the unit of m/s ^ 2.
The coordinate system is the same as the acceleration sensor.
When the device is reset, the output of the gravity sensor is the same as that of the acceleration sensor.
Linear accelerometer (LA-sensor.
The linear accelerometer is the data obtained by the accelerometer minus the gravity impact.
The Unit is m/s ^ 2, and the coordinate system is the same as the acceleration sensor.
The formula for calculating the accelerometer, gravity sensor, and linear accelerometer is as follows:
Acceleration = gravity + linear acceleration
Rotating vector sensor: RV-sensor.
The rotating vector represents the direction of the device and is a data obtained by mixing the coordinate axis and angle.
RV-sensor outputs three data records:
X * sin (theta/2)
Y * sin (theta/2)
Z * sin (theta/2)
Sin (theta/2) is the order of magnitude of RV.
The direction of the RV is the same as that of the axis rotation.
The three values of the RV, which are a triplet with cos (theta/2.
The RV data has no unit, and the coordinate system used is the same as the acceleration.
Example:
Sensors_event_t.data [0] = x * sin (theta/2)
Sensors_event_t.data [1] = y * sin (theta/2)
Sensors_event_t.data [2] = z * sin (theta/2)
Sensors_event_t.data [3] = cos (theta/2)
Reference: http://my.oschina.net/jerikc/blog/167499
Please indicate the author xiong_it and link: http://blog.csdn.net/xiong_it/article/details/45917009