Android sensor Overview (8)

Source: Internet
Author: User

Sensor Coordinate System

Generally, the sensor framework uses a standard 3-axis coordinate system to express data values. For most sensors, the coordinate system is defined relative to the screen of the device when the device is kept in the default direction (1 ). When the device is kept in the default direction, the X axis is horizontal to the right, the Y axis is vertical up, And the Z axis is pointed to the outside of the screen panel. In this system, the Z axis coordinates of the backfacing screen are negative values. The coordinate system is used by the following sensors:

1. Acceleration Sensor

2. Gravity Sensor

3. Gyroscope

4. Linear Acceleration Sensor

5. Magnetic instruments

Figure 1. coordinate system used by the sensor API.

The most important thing to understand is that when the orientation of the device screen changes, the coordinate axes of the coordinate system do not change, that is, the coordinate system of the sensor does not change because of the movement of the device. This behavior is the same as that of the OpenGL coordinate system.

In addition, the application should not assume that the natural (default) Direction of the device is vertical. For many tablet devices, the natural direction is horizontal. The sensor coordinate system is based on the natural direction of the device.

Finally, if your application needs to match the sensor data with the display on the screen, it needs to use the getRotation () method to determine the screen rotation, and then use remapCoordinateSystem () the method maps the sensor coordinates to the screen coordinates. Even if you specify a vertical display in your list, you need to do the same.

Note: The coordinate system used by some sensors and methods is relative to the global reference system (not the device Reference System ). The data returned by these sensors and methods is relative to the device motion or device location of the Earth.

 

Best practices for accessing and using sensors

When you design your sensor implementation, be sure to follow the following discussion guide. These guidelines are recommended best practices for accessing sensors and obtaining sensor data using sensor frameworks. Www.2cto.com

Unregister sensor listener

When the Activity used to complete the sensor or sensor is suspended, ensure that the sensor listener is deregistered. If the registered sensor listener and its Activity are suspended, the sensor will continue to obtain data and consume battery resources until the sensor is deregistered. The following code unregisters the listener using the onPause () method:

Private SensorManager mSensorManager;

...

@ Override

Protected void onPause (){

Super. onPause ();

MSensorManager. unregisterListener (this );

}

Do not test on the simulator

Because the simulator cannot simulate sensors, the sensor Code cannot be tested on the simulator. You must test your sensor code on a physical device. However, the sensor simulator can simulate the output of the sensor.

Do not block the onSensorChanged () method

Sensor data can change frequently, which means the system will frequently call the onSensorChanged (SensorEvent) method. As a best practice, do as little work as possible in the onSensorChanged (SensorEvent) method to avoid blocking this method. If the application needs to filter data or reduce sensor data, it should be executed outside of the onSensorChanged (SensorEvent) method.

Avoid using obsolete methods or sensor types

Several methods and constants have been discarded, especially the TYPE_ORIENTATION sensor type. Use the getOrientation () method to obtain direction data. Likewise, the TYPE_TEMPERATURE sensor type has been deprecated. Apply the TYPE_AMBIENT_TEMPERATURE sensor type to devices running Android4.0.

Confirm the sensor before use

Before trying to get data from a sensor, always check whether the sensor you are using exists on the device. It is not because a sensor is often used that simply assumes that the sensor exists. Device manufacturers are not required to provide any specific sensors on their devices.

Carefully select the sensor latency

When registering a sensor using the registerListener () method, You must select a sending frequency suitable for the application or application scenario. The sensor can provide data at a high frequency. The system is allowed to send additional data without wasting system resources or using battery power.

 

 

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.