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 type 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
A gravity sensor with version 3 is listed in 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.

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.