中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Identifying Sensors and Sensor Capabilities

發布時間:2020-07-13 00:56:13 來源:網絡 閱讀:506 作者:weijianzhenli 欄目:移動開發

Identifying Sensors and Sensor Capabilities

識別傳感器及其功能


The Android sensor framework provides several methods that make it easy for you to determine at runtime which sensors are on a device. The API also provides methods that let you determine the capabilities of each sensor, such as its maximum range, its resolution, and its power requirements.

To identify the sensors that are on a device you first need to get a reference to the sensor service. To do this, you create an instance of the SensorManager class by calling the getSystemService() method and passing in the SENSOR_SERVICE argument. For example:

    識別設備上的傳感器你首先需要得到 sensor service 的一個引用。為了得到這個引用,你調用getSystemService()方法并傳遞SENSOR_SERVICE參數來獲得 SensorManager 類的實例。

private SensorManager mSensorManager;
...
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 type constant for a specific sensor. If a device has more than one sensor of a given type, one of the sensors must be designated as the default sensor. If a default sensor does not exist for a given type of sensor, the method call returns null, which means the device does not have that type of sensor. For example, the following code checks whether there's a magnetometer on a device:

你也可以用getDefaultSensor(),傳遞特定傳感器的類型常量來確定某種傳感器是否在設備上。如果這種傳感器不止一個,一定指定其中一個為默認的傳感器。如果某個特定類型的默認傳感器不存在,這個方法返回null,表示設備上不存在這種傳感器。例如以下的代碼檢測設備上是否有磁力計。

private SensorManager mSensorManager;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
  // Success! There's a magnetometer.
  }
else {
  // Failure! No magnetometer.
  }

Note: Android does not require device manufacturers to build any particular types of sensors into their Android-powered devices, so devices can have a wide range of sensor 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 individual sensors. This is useful if you want your application to behave differently based on which sensors or sensor 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 for different manufacturer's sensors or different versions of a sensor. For example, if your application needs to monitor user gestures such as tilt and shake, you could create one set of data filtering rules and optimizations for newer devices that have a specific vendor's gravity sensor, and another set of data filtering rules and optimizations for devices that do not have a gravity sensor and have only an accelerometer. The following code sample shows you how you can use the getVendor() and getVersion() methods to do this. In this sample, we're looking for a gravity sensor that lists Google Inc. as the vendor and has a version number of 3. If that particular sensor is not present on the device, we try to use the accelerometer.


private SensorManager mSensorManager;
private Sensor mSensor;

...

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

if (mSensorManager.getDefaultSensor(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.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  }
  else{
    // Sorry, there are no accelerometers on your device.
    // You can't play this game.
  }
}

Another useful method is the getMinDelay() method, which returns the minimum time interval (in microseconds) a sensor can use to sense data. Any sensor that returns a non-zero value for the getMinDelay()method is a streaming sensor. Streaming sensors sense data at regular intervals and were introduced in Android 2.3 (API Level 9). If a sensor returns zero when you call the getMinDelay() method, it means the sensor is not a streaming sensor because it reports data only when there is a change in the parameters it is sensing.

The getMinDelay() method is useful because it lets you determine the maximum rate at which a sensor can acquire data. If certain features in your application require high data acquisition rates or a streaming sensor, you can use this method to determine whether a sensor meets those requirements and then enable or disable the relevant features in your application accordingly.

Caution: A sensor's maximum data acquisition rate is not necessarily the rate at which the sensor framework delivers sensor data to your application. The sensor framework reports data through sensor events, and several factors influence the rate at which your application receives sensor events. For more information, see Monitoring Sensor Events.


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

光泽县| 连南| 宿州市| 丰宁| 全州县| 横峰县| 陵川县| 墨玉县| 开封县| 宽城| 左权县| 甘谷县| 钟山县| 邯郸县| 瑞昌市| 甘孜县| 军事| 葫芦岛市| 陆河县| 色达县| 荣昌县| 临沧市| 乌恰县| 临潭县| 大英县| 海口市| 新化县| 含山县| 舒兰市| 民乐县| 保靖县| 定远县| 屏东市| 化州市| 永济市| 重庆市| 成都市| 彩票| 巴南区| 特克斯县| 双鸭山市|