接收设备GATT通知
通过API接收设备通知(Notification)/指示(Indication)数据。
参考 Receive Notification and Indication。
示例
1import asyncio
2import cassiablue
3
4
5async def notifier():
6 print("start recv notification data")
7 ok, _ = await cassiablue.start_recv_notify()
8 print(type(ok), ok)
9 if not ok:
10 return
11
12 async for evt in cassiablue.notify_result():
13 print(type(evt), evt)
14
15
16async def connector():
17 """
18 The handle and value in the examples relate to the device’s GATT service;
19 refer to the device-specific implementation protocol for details.
20 """
21 print("start connect")
22
23 params = '{"type": "random"}'
24 addr = "20:00:00:00:00:01"
25 ok, ret = await cassiablue.connect(addr, params)
26 print("connect done:", type(ok), ok, type(ret), ret)
27
28 await asyncio.sleep(3)
29
30 # open notification
31 handle = "45"
32 value = "0100"
33 ok, ret = await cassiablue.gatt_write(addr, handle, value)
34 print("write done:", type(ok), ok, type(ret), ret)
35
36 # write cmd trigger notification
37 handle = "42"
38 value = "20020001"
39 ok, ret = await cassiablue.gatt_write(addr, handle, value)
40 print("write done:", type(ok), ok, type(ret), ret)
41
42 await asyncio.sleep(5)
43 print("sleep ok")
44
45 await cassiablue.disconnect(addr)
46 print("disconnect ok")
47
48
49async def main():
50 print("start")
51 await asyncio.gather(notifier(), connector())
52
53
54print("hello")
55asyncio.run(main())
API
- async cassiablue.start_recv_notify() tuple[ok: bool, result: str]
开启接收设备GATT通知数据,目前支持同时开启1个
- 返回:
ok: 是否执行成功
result: 执行结果,消息数据请参考
BLENotifyResult
- async cassiablue.notify_result() BLENotifyResult
接收设备GATT通知数据
- 返回:
class BLENotifyResult
- class BLENotifyResult
异步可迭代对象,用于流式接收 BLE 通知数据。
- async __aiter__() BLENotifyResult
返回自身,使实例可直接用于
async for
循环。