我正在使用 PyQT5 库在 Windows 下编写 BLE 客户端。我能够使用以下代码快照发现所有服务:
if self.controller is None:
self.controller = QtBluetooth.QLowEnergyController.createCentral(self.currentDevice.get_device())
self.controller.connected.connect(self.controller_connected_handler)
self.controller.error.connect(self.controller_error_handler)
self.controller.disconnected.connect(self.controller_disconnected_handler)
self.controller.serviceDiscovered.connect(self.controller_service_discovered_handler)
self.controller.discoveryFinished.connect(self.controller_discovery_finished_handler)
if self.is_random_address():
logging.info("Random Address")
self.controller.setRemoteAddressType(QtBluetooth.QLowEnergyController.RandomAddress)
else:
logging.info("Public Address")
self.controller.setRemoteAddressType(QtBluetooth.QLowEnergyController.PublicAddress)
self.controller.connectToDevice()
每次发现新服务时都会执行“controller_service_discovered_handler”回调;
@pyqtSlot(QtBluetooth.QBluetoothUuid)
def controller_service_discovered_handler(self, service_uuid: QtBluetooth.QBluetoothUuid):
service = self.controller.createServiceObject(service_uuid)
if not service:
logging.warning('Warning: Cannot create service for uuid')
return
service_info = ServiceInfo(service)
# Note that ServiceInfo is a user-defined wrapper
logging.debug(f"New service was discovered: {service_info.serviceName} {service_info.serviceUuid}")
self.m_services.append(service_info)
但是,我想知道“PyQT5”库是否支持通过 UUID 发现特定的 GATT 服务,而不是使用基于回调的方法发现所有服务?(我需要这个,因为一些 BLE 设备手册建议了特定的服务发现方法,我需要按照建议的流程来解决我目前面临的一些问题)
[链接到底层 C++ QLowEnergyController 实现] https://doc.qt.io/qt-5/qlowenergycontroller.html