Identifying Sensors and Sensor Capabilities
识别传感器及其功能
The Android sensor framework provides several methods that make it easy for you to determine atruntime which sensors are on a device. The API also provides methods that let you determine thecapabilities of each sensor, such as its maximum range, its resolution, and its powerrequirements.
To identify the sensors that are on a device you first need to get a reference to the sensorservice. To do this, you create an instance of the SensorManager
class bycalling the getSystemService()
method and passingin the SENSOR_SERVICE
argument. For example:
识别设备上的传感器你首先需要得到 sensor service 的一个引用。为了得到这个引用,你调用getSystemService()方法并传递SENSOR_SERVICE
参数来获得 SensorManager 类的实例。
privateSensorManagermSensorManager;...mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
Next, you can get a listing of every sensor on a device by calling the getSensorList()
method and using the TYPE_ALL
constant. For example:
然后你可以用 getSensorList() 列举设备上所有传感器。
List<Sensor>deviceSensors=mSensorManager.getSensorList(Sensor.TYPE_ALL);
If you want to list all of the sensors of a given type, you could use another constant instead of TYPE_ALL
such as TYPE_GYROSCOPE
,TYPE_LINEAR_ACCELERATION
, orTYPE_GRAVITY
.
也可传递其他参数类列列举特定类型的所有传感器。
You can also determine whether a specific type of sensor exists on a device by using the getDefaultSensor()
method and passing in the typeconstant for a specific sensor. If a device has more than one sensor of a given type, one of thesensors must be designated as the default sensor. If a default sensor does not exist for a giventype of sensor, the method call returns null, which means the device does not have that type ofsensor. For example, the following code checks whether there's a magnetometer on a device:
你也可以用getDefaultSensor(),传递特定传感器的类型常量来确定某种传感器是否在设备上。如果这种传感器不止一个,一定指定其中一个为默认的传感器。如果某个特定类型的默认传感器不存在,这个方法返回null,表示设备上不存在这种传感器。例如以下的代码检测设备上是否有磁力计。
privateSensorManagermSensorManager;...mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);if(mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)!=null){//Success!There'samagnetometer.}else{//Failure!Nomagnetometer.}
Note: Android does not require device manufacturers to build anyparticular types of sensors into their Android-powered devices, so devices can have a wide range ofsensor configurations.
Android 不强求制造商在他们的Android设备上搭载某些传感器,所以设备上的传感器类别并不固定。
In addition to listing the sensors that are on a device, you can use the public methods of the Sensor
class to determine the capabilities and attributes of individualsensors. This is useful if you want your application to behave differently based on which sensors orsensor capabilities are available on a device. For example, you can use the getResolution()
and getMaximumRange()
methods to obtain a sensor's resolution and maximum range of measurement. You can also use the getPower()
method to obtain a sensor's power equirements.
你不仅可以列举设备上的所有传感器,还可以用Sensor 类的公共方法确定传感器的功能和属性。如果你的应用根据某个传感器或者某些传感器功能而有不同行为,这会非常有用。例如,用 getResolution() 和 getMaximumRange() 方法获得一个探测器的分辨率和测量的最大范围。你也可以用getPower()方法获得传感器的电力需求。
Two of the public methods are particularly useful if you want to optimize your application fordifferent manufacturer's sensors or different versions of a sensor. For example, if your applicationneeds to monitor user gestures such as tilt and shake, you could create one set of data filteringrules and optimizations for newer devices that have a specific vendor's gravity sensor, and anotherset of data filtering rules and optimizations for devices that do not have a gravity sensor and haveonly an accelerometer. The following code sample shows you how you can use the getVendor()
and getVersion()
methods to dothis. In this sample, we're looking for a gravity sensor that lists Google Inc. as the vendor andhas a version number of 3. If that particular sensor is not present on the device, we try to use theaccelerometer.
privateSensorManagermSensorManager;privateSensormSensor;...mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);if(mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)!=null){List<Sensor>gravSensors=mSensorManager.getSensorList(Sensor.TYPE_GRAVITY);for(inti=0;i<gravSensors.size();i++){if((gravSensors.get(i).getVendor().contains("GoogleInc."))&&(gravSensors.get(i).getVersion()==3)){//Usetheversion3gravitysensor.mSensor=gravSensors.get(i);}}}else{//Usetheaccelerometer.if(mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)!=null){mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);}else{//Sorry,therearenoaccelerometersonyourdevice.//Youcan'tplaythisgame.}}
Another useful method is the getMinDelay()
method,which returns the minimum time interval (in microseconds) a sensor can use to sense data. Any sensorthat returns a non-zero value for the getMinDelay()
method is a streamingsensor. Streaming sensors sense data at regular intervals and were introduced in Android 2.3 (APILevel 9). If a sensor returns zero when you call the getMinDelay()
method, it means thesensor is not a streaming sensor because it reports data only when there is a change in theparameters it is sensing.
The getMinDelay()
method is useful because it letsyou determine the maximum rateat which a sensor can acquire data. If certain features in your application require high dataacquisition rates or a streaming sensor, you can use this method to determine whether a sensormeets those requirements and then enable or disable the relevant features in your applicationaccordingly.
Caution: A sensor's maximum data acquisition rate is notnecessarily the rate at which the sensor framework delivers sensor data to your application. Thesensor framework reports data through sensor events, and several factors influence the rate at whichyour application receives sensor events. For more information, see Monitoring Sensor Events.
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。