Development of sensor applications on the Android platform-Basic Knowledge

Source: Internet
Author: User

Sensors on the Android platform include the accelerometer, orientation, magnetic field, and light ).

The development of sensor applications on the Android platform is implemented through the listener mechanism. The main steps to develop applications for one or more sensors are as follows:

1. Create a sensormanager object

2. Implement the sensorlistener Interface

This is the main task of developing sensor applications. To implement the sensorlistener interface, you need to implement the following two methods.

  • Void onaccuracychanged (INT sensor, int accuracy): This method is called when the sensor's precision changes. sensormanager provides three types of precision: sensor_status_accuracy_high, sensor_status_accuracy_medium, and sensor_status_accuracy_low. The parameter accuracy is a new precision.
  • Void onsensorchanged (INT sensor, float [] values): This method is called when sensor data changes. The main business code for developing sensor applications should be executed here, for example, read data and perform operations based on data changes. The sensor parameter passed in by the method is a constant representing the sensor type, and values is a float type array. Its length and content change depending on the sensor type.

3. Register sensorlistener

  • After developing and playing the sensorlistener, the rest of the work is to register and cancel the listener at the appropriate position of the program. Call the registerlistener method of the sensormanager object obtained in step 1 to register the listener. The received parameters include the listener object, sensor type, and frequency of sensor event transmission.
  • Call the unregisterlistener method of sensormanager when canceling sensorlistener registration. Generally, the registration and cancellation methods should appear in pairs, if the sensorlistener listener is registered in the onresume method of the activity, the registration should be canceled in the onpause method.

Introduction to common Sensors

1>. Acceleration Sensor Accelerometer

The accelerometer mainly senses the motion of the mobile phone. After registering the sensor listener, the accelerometer mainly captures three parameters: Values [0], values [1], and values [2].

Values [0]: The acceleration on the X axis minus the gravity acceleration minus the neutral acceleration on the X axis in the spatial coordinate system.

Values [1]: The acceleration on the X axis minus the gravity acceleration minus the neutral acceleration on the Y axis in the spatial coordinate system.

Values [2]: the acceleration in the X axis minus the gravity acceleration minus the neutral acceleration on the Z axis in the spatial coordinate system.

The units of the preceding three data items are meters per second.

Distance description:

1. when the mobile phone is still on the desktop, the acceleration is the gravity acceleration g, and the value of Values [2] is g by subtracting-g from 0 (the gravity acceleration g direction is the inverse direction of the Z axis, so it is a negative value.

2. If you push the mobile phone horizontally to the right, the acceleration on the mobile phone X is positive, that is, values [0] is positive.

3. when the mobile phone is raised to the vertical value of the acceleration of a meter per second, the return value of Values [2] is (a + G) meter per second, the result is obtained by subtracting-g from.

2>. Pose sensor Orientation

The pose sensor mainly senses the changes in the mobile phone's orientation. Each time it reads a static status value, after registering the sensor listener, the pose sensor mainly captures three parameters: Values [0], values [1], and values [2]. it represents the angle from which the mobile phone is rotated along the yaw axis, the pitch axis, and the roll axis respectively.

1. the yaw axis is the only unchanged axis in the three direction axes. Its direction is always vertical and upward. It is equivalent to the Z axis in the spatial coordinate system, that is, the opposite direction of the neutral acceleration G.

2. The orientation of the pitch axis depends on the rotation of the mobile phone along the yaw axis. That is, when the mobile phone turns a certain angle along the yaw axis, the pitch axis also turns the same angle around the yaw axis. The position of the pitch axis depends on the angle that the mobile phone rotates along the yaw axis. For example, the yaw axis and the pitch axis are two iron bars welded together to form a 90-degree angle.

3. Although the direction of the roll axis depends on the rotation of the mobile phone along the yaw axis and the pitch axis, the determination of the roll axis is not complicated. The relative relationship between the roll axis and the mobile phone is fixed. It is always in the positive direction along the long side of the phone screen.

Note:

1. the yaw axis is always in the vertical and upward direction, because no matter how the plane rotates, rolls, or the pilot must determine the upward direction when driving the plane.

2. the pitch axis will rotate along the yaw axis. In fact, the orientation of the pitch axis can be calculated. The rotation around the pitch axis indicates that the plane is pulled up or pushed down (head down)

3. The roll axis indicates the axis that runs through the fuselage. It is relatively static with the plane. The plane rotates left or right around the roll axis.

3>. Other Sensors

1. Magnetic Field Sensor Magnetic Field

The magnetic field sensor is mainly used to sense the surrounding magnetic induction intensity. After registering the listener, it mainly captures three parameters: Values [0], values [1], and values [2]. The three parameters represent the components of the magnetic induction intensity on the three direction axes in the spatial coordinate system. The units of all data are ut, that is, microtelas.

2. Light Sensor

The optical sensor is used to sense the surrounding light intensity. After registering the listener, only one parameter is captured: Values [0]. This parameter indicates the surrounding illumination intensity, in Lux ).

3. Proximity

The distance sensor is used to sense nearby objects. After registering the listener, only one parameter is captured: Values [0]. This parameter indicates the distance of the sensor, in centimeters.

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.