Android sensor Overview (5)

Source: Internet
Author: User

Ability to identify sensors and sensors

The Android sensor framework provides several methods that allow you to easily determine the sensors on the device during running. The API also provides several methods for you to determine the capabilities of each sensor, such as the maximum range, recognition rate, and power requirement.

To identify sensors on a device, you must first obtain a reference sensor service. Call the getSystemService () method and pass the SENSOR_SERVICE parameter to create an instance of the SensorManager class to obtain a sensor service. For example:

Private SensorManager mSensorManager;

...

MSensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE );

Next, call the getSensorList () method with the TYPE_ALL constant to obtain the list of all sensors on the device. For example:

List <Sensor> deviceSensors = mSensorManager. getSensorList (Sensor. TYPE_ALL );

If you want to list the sensors of a given type, you can use another constant instead of TYPE_ALL, such as TYPE_GYROSCOPE, TYPE_LINEAR_ACCELERATION, or TYPE_GRAVITY.

By using the getdefasensensor method, you can also determine whether a specified type of sensor exists on the device. If the device has multiple sensors of the specified type, make sure that there is a default sensor. If the specified sensor type does not have a default sensor, null is returned for this method call, which means that this type of sensor does not exist on the device. For example, run the following code to check whether a magnetic meter exists on the device:

Private SensorManager mSensorManager;

...

MSensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE );

If (mSensorManager. getdefasensensor (Sensor. TYPE_MAGNETIC_FIELD )! = Null ){

// Success! There's a magnetometer.

}

Else {

// Failure! No magnetometer.

}

Note: Android does not require manufacturers to include any special types of sensors in their Android devices, so devices can have a wide range of sensor configurations.

In addition to listing the sensors on the device, you can use the Public method of the Sensor class to determine the capabilities and attributes of individual sensors. This is useful if you want your applications to behave differently from the sensors available on basic sensors or devices. For example, the getResolution and getMaximumRange () methods can be used to obtain the sensor's recognition rate and maximum measurement range, and the getPower method can also be used to obtain the power requirements of the sensor.

Two public methods are particularly useful if you want to optimize your applications for sensors of different manufacturers or for sensors of different versions. For example, if your application needs to monitor user gestures such as swing and vibration, you can create a set of data optimization filtering rules for devices with relatively new vector gravity sensors, and another set of data filtering rules for devices that do not have a gravity sensor and only have an acceleration sensor. The following code example demonstrates how to use the getVendor () and getVersion () methods to do this. In this example, we find the gravity sensor with version 3 listed in Google Inc. If the sensor does not exist on the device, we try to use the accelerometer.

Private SensorManager mSensorManager;

Private Sensor mSensor;

 

...

 

MSensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE );

 

If (mSensorManager. getdefasensensor (Sensor. TYPE_GRAVITY )! = Null ){

List <Sensor> gravSensors = mSensorManager. getSensorList (Sensor. TYPE_GRAVITY );

For (int I = 0; I <gravSensors. size (); I ++ ){

If (gravSensors. get (I). getVendor (). contains ("Google Inc ."))&&

(GravSensors. get (I). getVersion () = 3 )){

// Use the version 3 gravity sensor.

MSensor = gravSensors. get (I );

}

}

}

Else {

// Use the accelerometer.

If (mSensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER )! = Null ){

MSensor = mSensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER );

}

Else {

// Sorry, there are no accelerometers on your device.

// You can't play this game.

}

}

Another useful method is getMinDelay (), which returns the minimum interval (in milliseconds) of sensor-aware data ). The sensor that returns a non-zero value using the getMinDelay () method is a flow sensor. The sensor periodically perceives data and is introduced in Android2.3 (API Level 9. If 0 is returned when the getMinDelay () method is called, this means that the sensor is not a flow sensor because it only reports the data it perceives when it changes.

The getMinDelay () method is useful because it allows you to determine the maximum frequency at which a sensor can obtain data. If a feature in your application requires a high-precision frequency or flow sensor, you can use this method to determine whether a sensor meets these requirements, enable or disable related functions in your application.

Warning the maximum data acquisition frequency of a sensor is not necessarily the frequency at which the sensor framework sends sensor data to your application. The sensor framework reports data through sensor events, and several factors affect the frequency of applications receiving sensor events.

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.