如何解决如果通知和指示属性存在于 Python 中的一个特征中
我正在使用 bleak 来控制 Python 中的 BLE(蓝牙低功耗)设备。 并且我试图控制一个 BLE 设备,该设备在一个特性中包含通知和指示属性,因此出现了仅指示被激活的问题。
所以当我查看了 bleak 的源代码时,发现如果存在,则将其激活为指示,如果不存在,则检查通知并将其激活为通知,并且 start_notify 有效。
以下是相关源码和链接。
https://github.com/hbldh/bleak/blob/develop/bleak/backends/winrt/client.py
async def start_notify(
self,char_specifier: Union[BleakGATTCharacteristic,int,str,uuid.UUID],callback: Callable[[int,bytearray],None],**kwargs
) -> None:
...
...
characteristic_obj = characteristic.obj
if (
characteristic_obj.characteristic_properties
& GattCharacteristicProperties.INDICATE
):
cccd = GattClientCharacteristicConfigurationDescriptorValue.INDICATE
elif (
characteristic_obj.characteristic_properties
& GattCharacteristicProperties.NOTIFY
):
cccd = GattClientCharacteristicConfigurationDescriptorValue.NOTIFY
else:
cccd = GattClientCharacteristicConfigurationDescriptorValue.NONE
fcn = _notification_wrapper(bleak_callback,asyncio.get_event_loop())
event_handler_token = characteristic_obj.add_value_changed(fcn)
self._notification_callbacks[characteristic.handle] = event_handler_token
status = await characteristic_obj.write_client_characteristic_configuration_descriptor_async(
cccd
)
在这种情况下处理它的最佳方法是什么? 所以我实现了下面的代码来使用“通知”,但它看起来不太好。
async with BleakClient(address,timeout=5.0) as client:
client.set_disconnected_callback(on_disconnect)
services = await client.get_services()
for service in services:
for characteristic in service.characteristics:
if characteristic.uuid == read_write_charcteristic_uuid:
if 'notify' in characteristic.properties:
# if "indicate" is exist remove it
if 'indicate' in characteristic.properties:
characteristic.properties.remove('indicate')
# then "notify" becomes active.
await client.start_notify(notify_charcteristic,callback)
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。