Usage of common android Sensors

Source: Internet
Author: User

Reference: http://wikidroid.sinaapp.com/Guide/topics/sensors/sensors_overview

1. Sensor entry

Since Apple released its first iPhone in 2007, sensors that seem to be tied to mobile phones have gradually become an important part of mobile phone hardware. If you have used iPhone, HTC Dream, HTC magic, and HTC
Hero and Other Android phones will find that by placing the phone either horizontally or vertically, the screen will change with the mobile phone location. This feature requires Gravity Sensors. Apart from Gravity Sensors, many other types of sensors are applied to mobile phones. For example, reluctance sensors are the most important sensors. Although mobile phones can use GPS to determine the direction, GPS is useless when the GPS signal is poor or there is no GPS signal at all. At this time, the reluctance sensor can easily determine the direction (east, south, west, north ). With the reluctance sensor, it is also possible to make the electronic nature of the compass (commonly known as pointing to the needle.
The use of sensors in Android applications depends on the Android. Hardware. sensoreventlistener interface. This interface can be used to monitor various events of a sensor. The code for the sensoreventlistener interface is as follows:

  1. Package Android. hardware;
  2. Public interface sensoreventlistener
  3. {
  4. Public void
  5. Onsensorchanged (sensorevent event );
  6. Public void
  7. Onaccuracychanged (sensor, int accuracy );
  8. }

Copy code

Two methods are defined in the sensoreventlistener interface: onsensorchanged and onaccuracychanged. The onsensorchanged method is called when the sensor value changes, for example, the direction of the reluctance sensor changes. The onaccuracychanged method is called when the sensor's precision changes.
The onsensorchanged method has only one sensorevent type parameter event. The sensorevent class has a values variable, which is of the type float []. However, this variable has a maximum of three elements, and the elements in the values variable have different meanings depending on the sensor.

Before explaining the meaning of the elements in the values variable, let's first introduce how the android coordinate system defines the X, Y, and Z axes.

The X axis direction is from left to right along the horizontal direction of the screen. If the mobile phone is not square, the shorter side needs to be placed horizontally, and the longer side needs to be placed vertically.
The Y-axis direction is from the lower left corner of the screen along the vertical direction of the screen to the top of the screen.
Flat the cell phone on the desk, and direct the Z axis from the cell phone to the sky.

The following are the meanings of the values variable elements in the main sensors.

1.1 Direction Sensor

The three values of the values variable in the direction sensor indicate the degree. Their meanings are as follows:

Values [0]: This value indicates the orientation, that is, the angle from which the mobile phone rotates around the Z axis. 0 indicates North (North); 90 indicates East (East); 180 indicates South (South); 270 indicates West (West ). If the value of Values [0] is exactly the four values, and the mobile phone is placed horizontally, it indicates that the front of the mobile phone is the four directions. You can use this feature to implement the electronic compass. instance 76 will detail the implementation process of the Electronic Compass.


Values [1]: This value indicates the inclination or the mobile phone climbing. This value changes when the phone is tilted around the X axis. The value range of values [1] is-180 ≤ values [1].
≤ 180. Assume that the screen of the mobile phone is placed horizontally on the table. If the table is completely horizontal, the value of Values [1] should be 0 (since few tables are absolutely horizontal, this value may not be 0, but is generally a value between-5 and 5 ). At this time, the phone is raised from the top of the phone until it is rotated 180 degrees along the X axis (the screen is horizontally placed on the desktop ). During this rotation process, values [1] will change between 0 and-180. That is to say, when the value of Values [1] is lifted from the top of the mobile phone, it will gradually decrease, until it is equal to-180. If the mobile phone is raised from the bottom until it is rotated 180 degrees along the X axis, then values [1] will change between 0 and 180. That is, the value of Values [1] will gradually increase until it is equal to 180. You can use values [1] and the Values [2] to measure the inclination of objects such as tables.


Values [2]: indicates the scroll angle of the mobile phone along the Y axis. The value range is-90 ≤ values [2] ≤ 90. Assume that the screen of the mobile phone is placed horizontally on the desktop. If the desktop is flat, the value of Values [2] should be 0. When the left side of the phone is gradually raised, the value of Values [2] gradually decreases until the phone is placed perpendicular to the desktop. Then, the value of Values [2] is-90. When the right side of the phone is gradually raised, the value of Values [2] increases gradually until the phone is placed perpendicular to the desktop, then the value of Values [2] is 90. Scroll to the right or left when the vertical position is specified. The value of Values [2] will continue to change between-90 and 90.

1.2 Acceleration Sensor

The three element values of the values variable of the sensor represent the acceleration values of X, Y, and Z axes respectively. For example, if a mobile phone is horizontally placed on the desktop, it moves from the left to the right. Values [0] is a negative value, and values [0] is a positive value. Readers can use the examples in this section to understand the value changes in the accelerator sensor. To use the corresponding sensor, it is not enough to implement only the sensoreventlistener interface. You also need to use the following code to register the corresponding sensor.

  1. // Obtain the sensor Manager
  2. Sensormanager Sm = (sensormanager) getsystemservice (sensor_service );
  3. // Register the direction Sensor
  4. SM. registerlistener (this,
  5. SM. getdefasensensor (sensor. type_orientation ),
  6. Sensormanager. sensor_delay_fastest );

Copy code

To register another sensor, you can change the 1st parameter values of the getdefasensensor method. For example, you can use sensor. type_accelerometer to register an acceleration sensor. Many sensor constants are defined in the sensor class, but the sensor must be registered based on the actual hardware configuration on the mobile phone. If the mobile phone does not have the corresponding sensor hardware, even if the corresponding sensor is registered, it will not work. The getdefasensensor method's 2nd parameters indicate the speed at which the sensor data is obtained. Sensormanager. sensor_delay _
Fastest indicates obtaining sensor data as quickly as possible. In addition to this value, you can set three speed values for obtaining sensor data. These values are as follows:

Sensormanager. sensor_delay_normal: the default speed for obtaining sensor data.
Sensormanager. sensor_delay_game: this value is recommended if a game is developed using sensors.
Sensormanager. sensor_delay_ui: this value is recommended if you use a sensor to update data in the UI.

1.3 gravity Sensor

The type constant of the accelerometer is sensor. type_gravity. The gravity sensor and the accelerometer use the same coordinate system. The three elements in the values array indicate the gravity of the X, Y, and Z axes respectively. Android
The SDK defines constants used to represent the gravity of the planet, satellite, and sun surface in the galaxy. Next, let's take a look at the astronomical knowledge. In the future, if you use an Android phone outside the Earth, it may be useful.

  1. Public static final float gravity_sun = 275.0f;
  2. Public static final float gravity_mercury = 3.70f;
  3. Public static final float gravity_venus = 8.87f;
  4. Public static final float gravity_earth = 9.80665f;
  5. Public static final float gravity_moon = 1.6f;
  6. Public static final float gravity_mars = 3.71f;
  7. Public static final float gravity_jupiter = 23.12f;
  8. Public static final float gravity_saturn = 8.96f;
  9. Public static final float gravity_uranus = 8.69f;
  10. Public static final float gravity_neptune = 11.0f;
  11. Public static final float gravity_pluto = 0.6f;
  12. Public static final float gravity_death_star_ I = 0.000000000036145f;
  13. Public static final float gravity_the_island = 4.815162342f;

Copy code

1.4 light sensor

The type constant of the light sensor is sensor. type_light. The values array is meaningful only when the first element (Values [0]) is used. Indicates the intensity of light. The maximum value is 120000.0f. Android
The SDK divides the light intensity into different levels. The maximum value of each level is represented by a constant. These constants are defined in the sensormanager class. The Code is as follows:

  1. Public static final float light_sunlight_max = 120000.0f;
  2. Public static final float light_sunlight = 111080000f;
  3. Public static final float light_shade = 20000.0f;
  4. Public static final float light_overcast = writable F;
  5. Public static final float light_sunrise = 400366f;
  6. Public static final float light_cloudy = 100366f;
  7. Public static final float light_fullmoon = 0.25f;
  8. Public static final float light_no_moon = 0.001f;

Copy code

The above eight constants are only critical values. When using a light sensor, you must determine a range based on the actual situation. For example, when the sun gradually rises, the value of Values [0] is likely to exceed light_sunrise. When the value of Values [0] increases gradually, it will gradually jump over light_overcast, to reach light_shade, of course, if the day is particularly good, it may also reach light_sunlight, or even higher. 1.5 gyroscope Sensor
The type constant of the gyroscope sensor is sensor. type_gyroscope. The meanings of the three elements in the values array are as follows: Values [0]: the angular velocity of the X axis rotation.
Values [1]: returns the angular velocity of the Y axis rotation.
Values [2]: returns the angular velocity of the Z axis rotation.
When the mobile phone rotates counterclockwise, the angular velocity is positive. When the mobile phone rotates clockwise, the angular velocity is negative. Gyroscope sensors are often used to calculate the Rotation Angle of mobile phones. The Code is as follows:
  1. Private Static final float ns2s = 1.0f/Optional bytes 0.0f;
  2. Private float timestamp;
  3. Public void onsensorchanged (sensorevent event)
  4. {
  5. If (timestamp! = 0)
  6. {
  7. // Event. timesamp indicates the current time, in the unit of nanoseconds (one thousandth of milliseconds)
  8. Final float dt = (event. timestamp-timestamp) * ns2s;
  9. Angle [0] + = event. Values [0] * DT;
  10. Angle [1] + = event. Values [1] * DT;
  11. Angle [2] + = event. Values [2] * DT;
  12. }
  13. Timestamp = event. timestamp;
  14. }

Copy code

In the code above, the time difference (DT) between two neighboring gyroscope sensors is used to calculate the Rotation Angle of the X, Y, and Z axes of the mobile phone during this period, and add the values to different elements of the angle array.

 

 

 

1.6 other sensors
Other sensors are described in the previous sections as acceleration sensors, Gravity Sensors, light sensors, gyroscope sensors, and direction sensors. In addition to these sensors, the android SDK also supports the following sensors. For how to use these sensors and Their constants and methods, see the official documentation.

Short-range sensor (sensor. type_proximity)
Linear accelerometer (sensor. type_linear_acceleration)
Rotating vector sensor (sensor. type_rotation_vector)
Magnetic Field Sensor (sensor. type_magnetic_field)
Pressure Sensor (sensor. type_pressure)
Temperature Sensor (sensor. type_temperature)

Although the android SDK defines more than a dozen sensors, not every mobile phone fully supports these sensors. For example, Google Nexus S supports nine of these sensors (pressure and temperature sensors are not supported), while HTC
G7 only supports five of these sensors. If a sensor not supported by a mobile phone is used, no exception is thrown, but the data transmitted by the sensor cannot be obtained. When using sensors, it is best for readers to determine whether the current mobile phone supports the sensors used.

 

From: http://edu.gamfe.com/tutor/d/33591.html

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.