Receive device GATT notification
Receive device Notification/Indication data through the API.
Refer Receive Notification and Indication.
Example
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]
Enable receiving device GATT notification data. Currently, only one subscription can be active at a time.
- Returns:
ok: Whether the operation was successful
result: Execution result. For notification data, refer to
BLENotifyResult
- async cassiablue.notify_result() BLENotifyResult
Receive Device GATT Notification Data
- Returns:
class BLENotifyResult
- class BLENotifyResult
An asynchronous iterable object used to receive BLE notification data in a streaming manner.
- async __aiter__() BLENotifyResult
Returns itself, allowing the instance to be used directly in an
async for
loop.
- async __anext__() dict
Asynchronously retrieves the next notification data from the global
notify_sse_client.queue
and returns it.If the queue is empty, it waits until new data becomes available.
Data Field Description Field
Type
Description
Example
id
str
Device MAC
20:00:00:00:00:01
dataType
str
Data Type
notification
indication
handle
int
Handle
44
value
str
Value
A0EE00…