确定传感器是否存在并正常工作的可靠方法是什么?

huangapple go评论169阅读模式
英文:

What's the sure-fire way to check if a sensor exists and IS WORKING?

问题

我在 Stack Overflow 上看到了许多方法:

第一个方法:

PackageManager manager = getPackageManager();
boolean hasLightSensor = manager.hasSystemFeature(PackageManager.FEATURE_SENSOR_LIGHT);

另一个方法:

SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor light = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
if (light != null) {
    // ...
}

还有另一个方法:

SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor light = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
SensorEventListener listener = new SensorEventListener() {
    // 创建传感器监听器
};
boolean hasLightSensor = sm.registerListener(listener, light, SensorManager.SENSOR_DELAY_NORMAL);
英文:

I've seen many methods on SO:

PackageManager manager = getPackageManager();
boolean hasLightSensor = manager.hasSystemFeature(PackageManager.FEATURE_SENSOR_LIGHT);

And another:

SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor light = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
if (light != null) {
...
}

And another:

SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor light = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
SensorEventListener listener = new SensorEventListener() {
     // create sensor listener
}
boolean hasLightSensor = sm.registerListener(listener, light, SensorManager.SENSOR_DELAY_NORMAL);

答案1

得分: 2

在 Android 传感器错误处理中涉及许多类和方法。通过查看源代码来获取清晰的答案是最佳方法,有时这会进入一条兔子洞。

SensorManager 是一个抽象类,定义了获取传感器列表的方法。getDefaultSensor() 调用了 getSensorList()。如果没有找到传感器,getDefaultSensor 将返回 null。因此,Android 传感器最佳实践 页面建议在使用传感器之前进行存在性检查,如下所示:

if (sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE) != null) {
    // 成功!存在压力传感器。
} else {
    // 失败!不存在压力传感器。
}

您还需要验证 registerListener() 调用的返回值。registerListenerImpl 方法是在 SystemSensorManager 中实现的,它是一个扩展了 SensorManager 的具体类。registerListener 将会:

  • 如果任何输入参数无效,返回 false
  • 如果超过了 MAX_LISTENER_COUNT,抛出 IllegalStateException
  • 如果传感器无法添加到与提供的 SensorEventListener 关联的 SensorEventQueue,返回 false

最后一点很重要。BaseEventQueue 类(SensorEventQueue 的基类)提供了一个叫做 addSensor() 的方法,如果:

  • 传感器已经添加到队列并处于活动状态
  • 传感器无法支持所提供的批处理报告延迟
  • 传感器无法启用

这最后一点非常重要,因为返回值为 false 可能表示传感器故障。要了解更多有关导致传感器无法启用的原因,您需要查看 C++ 中的 Native Sensor Manager 代码。可以简单地说,您应该始终检查 registerListener() 调用的返回值。在使用完传感器后不要忘记取消注册。

英文:

There are quite a few classes and methods involved in Android sensor error handling. I've found that the best way to get clear answers is by looking at the source code, and sometimes that goes down a rabbit hole.

SensorManager is an abstract class that defines the methods to get the sensor list. getDefaultSensor()calls getSensorList(). If no sensors are found, getDefaultSensor will return null. The Android Sensor Best Practices page therefore recommends checking that sensors exist before using them, as follows:

if (sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE) != null) {
    // Success! There's a pressure sensor.
} else {
    // Failure! No pressure sensor.
}

You may also need to verify the return value of the registerListener() call. The registerListenerImpl methods are implemented in SystemSensorManager, which is a concrete class that extends SensorManager. registerListener will:

  • return false if any of the input arguments are invalid
  • throw an IllegalStateException if MAX_LISTENER_COUNT has been exceeded
  • return false if the sensor cannot be added to the SensorEventQueue associated with the provided SensorEventListener

This last point is important. The BaseEventQueue class (base class of SensorEventQueue) provides a method called addSensor(), which can return false if:

  • the sensor has already been added to the queue and is active
  • the sensor cannot support the provided batch report latency
  • the sensor could not be enabled

This last point is very important, since a return value of false may indicate a sensor failure. To find out more about what can cause the sensor to not enable, you need to look into the Native Sensor Manager code in C++. Suffice it to say, you should always check the return value of your registerListener() calls. Don't forget to unregister when you're done with the sensor.

huangapple
  • 本文由 发表于 2020年7月26日 18:39:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63099031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定