In this article, I'll share with you how Android devices are developed in Android.
Reprint Please specify author xiong_it and link: http://blog.csdn.net/xiong_it/article/details/45917009
basic process for Android sensor development
public class Sensoractivity extends Activity implements Sensoreventlistener {private Sensormanager msensormanager; Private Sensor msensor; @Override Public final void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); First step: Get Sensormanager Instance Object Msensormanager = (Sensormanager) getsystemservice (context.sensor_) via Getsystemservice SERVICE); The second step: Obtain the desired sensor object by Sensormanager The instance object: The parameter determines which sensor to obtain msensor = Msensormanager.getdefaultsensor (sensor.type_light); }//Fourth step: The two methods that must be rewritten: onaccuracychanged,onsensorchanged/** * Sensor accuracy changed callback interface */@Override public final void Onaccuracy Changed (sensor sensor, int accuracy) {//todo When sensor accuracy is changed, accuracy for current sensor accuracy}/** * Callback interface when sensor event value is changed: frequency of execution of this method with registered sensing * * @Override public final void onsensorchanged (Sensorevent event) {///most sensors will return an event value x,y,x the three axis direction, the meaning of the value is due to the sensor different float x = event.values[0]; Float y = event.values[1]; float z = event.values[2]; TODO takes advantage of the three availableFloat sensor value do something}/** * Step three: Register the sensor while gaining focus and let this class implement the Sensoreventlistener interface */@Override protected void Onresume () {Super . Onresume (); /* First parameter: instance object for Sensoreventlistener interface * Second parameter: sensor instance to register * Third parameter: Sensor gets sensor event value frequency: * sensormanager.sensor_delay_fastest = 0: update interval corresponding to 0 microseconds, fastest, 1 μs = 1 1 million sec * &N Bsp Sensormanager.sensor_delay_game = 1:20000 microsecond update interval, common in-game * & nbsp  SENSORMANAGER.SENSOR_DELAY_UI = 2: Update interval for 60000 microseconds * &NB Sp sensormanager.sensor_delay_normal = 3: Update interval corresponding to 200000 microseconds * & nbsp When you type a custom int value x: The update interval for x microseconds * */Msensormanager.registerlistener (this, msensor, sensormanager.sensor_d Elay_normal); }/** * Fifth: Unregisters the sensor when it loses focus */@Override protected void OnPause () {super.onpause (); MsensormAnager.unregisterlistener (this); }}
There are roughly 5 steps to sum up:First step: Get the Sensormanager objectStep Two: Get the Sensor objectStep Three: Register the Sensor objectFourth step: Rewrite the onaccuracychanged,onsensorchanged two methodsFifth Step: Unregister the Sensor objectbasic types of sensors in Android devices
Using Sensormanager to obtain the sensor:
Msensor = msensormanager.getdefaultsensor (int TYPE);//type is the static final value defined in sensor
Please refer to the section type values defined in Sensor.java below:
/** * Gets the constant of the accelerometer (*/public static final int type_accelerometer = 1; /** * Gets the constant of the magnetic field sensor */public static final int type_field = 2; /** * Gets the constant of the direction sensor, obsolete */@Deprecated public static final int type_orientation = 3; /** * Gets the constant of the gyroscope sensor */public static final int type_gyroscope = 4; /** * Gets the constant of the light sensor */public static final int type_light = 5; /** * Gets the constant of the pressure sensor */public static final int type_pressure = 6; @Deprecated public static final int type_temperature = 7;//Get temperature sensor, obsolete/** * Get constant of distance sensor */public static Final int type_proximity = 8; /** * Gets the constant of the gravity sensor */public static final int type_gravity = 9; /** * Gets the constant of the linear accelerometer/public static final int type_linear_acceleration = 10; /** * Gets the constant of the rotation vector sensor */public static final int type_rotation_vector = 11; /** * Gets the constant relative humidity sensor */public static final int type_relative_humidity = 12; /** *Get constant for temperature sensor: Banned public static final int type_temperature = 7; */public static final int type_ambient_temperature = 13;
Sensor Event value interpretation in Android devices
Acceleration SensorThe accelerometer is also called G-sensor, which returns the acceleration values for the X, y, and Z axes.
This value contains the effect of gravity, in units of m/s^2.
Put your phone flat on the desktop, the X axis defaults to the 0,y axis default 0,z axis default 9.81.
Place your phone face down on the desktop with a Z axis of-9.81.
Tilt the phone to the left and the x-axis as positive.
Tilt the phone to the right and negative x-axis.
Tilt the phone upward and the y-axis is negative.
Tilt the phone downward and the y-axis is positive.
Magnetic SensorsThe magnetic sensor is referred to as m-sensor and returns the environmental magnetic field data of the X, y, and Z axes.
The unit of the value is micro-Tesla (Micro-tesla), expressed in UT.
The unit can also be Gauss (Gauss), 1tesla=10000gauss.
There is generally no independent magnetic sensor on the hardware, and the magnetic data is provided by the electronic Compass Sensor (E-COMPASS).
The Electronic Compass sensor also provides direction sensor data for the following.
Direction SensorThe direction sensor is abbreviated as O-SENSOR, which returns the angle data of the three axes, and the unit of the direction data is the angle.
To get accurate angular data, e-compass needs to get g-sensor data,
The production o-sensor data is calculated, otherwise only the horizontal angle can be obtained.
The direction sensor provides three data, azimuth, pitch, and roll respectively.
Azimuth: Azimuth, returns the angle of the magnetic north and Y axes at horizontal time, ranging from 0° to 360°.
0°= North, 90°= East, 180°= South, 270°= West.
The angle between the pitch:x axis and the horizontal plane, ranging from -180° to 180°.
When the z axis rotates toward the y axis, the angle is positive.
The angle between the roll:y axis and the horizontal plane, for historical reasons, ranges from -90° to 90°.
When the x-axis moves toward the z-axis, the angle is positive.
The electronic compass needs to be calibrated before it can get the correct data, usually with a 8-word calibration method.
8-word calibration requires the user to do 8-word shaking in the air using a device that needs to be calibrated,
In principle, as much as possible, the device normal direction points to all 8 quadrants of space.
Gyro SensorThe gyroscope sensor, called gyro-sensor, returns angular velocity data for the x, y, and Z axes.
The unit of angular velocity is radians/second.
Measured by Nexus S mobile:
Rotate horizontally counterclockwise and the z axis is positive.
The horizontal counterclockwise rotation, the z axis is negative.
Rotate to the left and negative on the Y axis.
Rotates to the right and the y-axis is positive.
Rotate up and the x axis is negative.
Rotate downward and the x axis is positive.
Light Sensing sensorThe light sensor detects real-time light intensity, the unit of light is lux, and its physical meaning is luminous flux on the unit area.
The light sensor is mainly used for the LCD auto-brightness function of Android system.
The brightness of the LCD can be adjusted in real time according to the light intensity sampled.
Pressure sensorThe pressure sensor returns the current pressure in the unit of Hectopascal (HPa).
Temperature SensorThe temperature sensor returns the current temperature.
Distance sensoralso known as proximity sensors, the detection of the distance between the object and the cell, the unit is centimeters.
Some proximity sensors can only return far and near two states,
Therefore, the proximity sensor returns the maximum distance to the far state, which is less than the maximum distance to return to the near state.
Proximity sensors can be used to automatically turn off the LCD screen when answering a phone to conserve power.
Some chips have integrated proximity sensors and light sensors for both functions.
Gravity sensorThe gravity sensor is referred to as gv-sensor and outputs gravity data.
On Earth, the value of gravity is 9.8, the unit is m/s^2.
The coordinate system is the same as the accelerometer sensor.
When the device is reset, the output of the gravity sensor is the same as the accelerometer sensor.
Linear acceleration SensorLinear accelerometer is referred to as la-sensor.
The linear accelerometer is the data obtained by the acceleration sensor minus the gravity effect.
The unit is m/s^2 and the coordinate system is the same as the accelerometer sensor.
The formula for acceleration sensors, gravity sensors and linear accelerometer is as follows:
Acceleration = gravity + Linear acceleration
rotational vector SensorsRotary vector sensor for short rv-sensor.
The rotation vector represents the direction of the device, which is a mix of axes and angles to calculate the data.
Rv-sensor output of three data:
X*sin (THETA/2)
Y*sin (THETA/2)
Z*sin (THETA/2)
Sin (THETA/2) is an order of magnitude for RV.
The RV is in the same direction as the Axis rotation.
The three values of the RV, together with the Cos (THETA/2), constitute a four-tuple.
RV data has no units, and the coordinate system used is the same as 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 Link: http://my.oschina.net/jerikc/blog/167499
Reprint Please specify author xiong_it and link: http://blog.csdn.net/xiong_it/article/details/45917009
Android Development: Android device Sensor Development Summary