动态传感器后台注册

静态后端注册

传感器后端通常是静态注册的。首次使用传感器库时进行注册,程序运行时注册仍然有效。

静态注册的后端仍可能表现出一些动态行为,因为QSensorBackendFactory 可以返回 0 表示无法创建后端。

动态后端注册

虽然静态注册对大多数后端都没有问题,但在某些情况下,静态注册会产生问题。

最明显的例子就是代表非固定硬件的后端。例如,通过蓝牙连接的游戏控制器。由于手机范围内可能有不止一个游戏控制器,程序希望记录应使用的特定游戏控制器。如果后端是静态注册的,就不会有关于控制器的唯一信息。相反,注册会被延迟,直到看到控制器为止。

建议的注册策略

固定硬件的后端应立即注册。应用程序可以看到传感器可以使用。

远程硬件的后端不应立即注册。应用程序可以看到传感器无法使用。当远程硬件可用时,应注册后端。应用程序可以看到传感器现在可用。

如果需要从工厂为已注册的后端返回 0,则应取消后端注册。应用程序可以看到传感器不再可用。如果工厂可以再次创建后端,则应重新注册。应用程序可以看到传感器再次可用。

当底层硬件不再可用时,应取消注册后端。后端现有实例应向应用程序报告错误状态,但应妥善处理。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.