Connection State Event

Obtain device connection state events through the API.

Refer Get Device Connection Status.

Note

  • If the device is already in a connected state, the connection API will return a success response, but no connection event will be reported.

Example

 1import asyncio
 2import cassiablue
 3
 4
 5async def stater():
 6    print("start recv state event")
 7    ok, _ = await cassiablue.start_recv_connection_state()
 8    print(type(ok), ok)
 9    if not ok:
10        return
11
12    async for evt in cassiablue.connection_result():
13        print(type(evt), evt)
14
15
16async def connector():
17    print("start connect")
18
19    params = '{"type": "random"}'
20    addr = "20:00:00:00:00:01"
21    ok, ret = await cassiablue.connect(addr, params)
22    print("connect done:", type(ok), ok, type(ret), ret)
23
24    await asyncio.sleep(3)
25
26    ok, ret = await cassiablue.disconnect(addr)
27    print("disconnect done:", type(ok), ok, type(ret), ret)
28
29
30async def main():
31    print("start")
32    await asyncio.gather(stater(), connector())
33
34
35print("hello")
36asyncio.run(main())

API

async cassiablue.start_recv_connection_state() tuple[ok: bool, result: str]

Enable retrieval of device connection state events to receive real-time updates of device connection status changes. Currently, only one subscription can be active at a time.

Returns:

  • ok: Whether the operation was successful

  • result: Execution result. For state event, refer to BLEConnectionResult

async cassiablue.connection_result() BLEConnectionResult

Retrieve Connection State Event

Returns:

class BLEConnectionResult

class BLEConnectionResult

An asynchronous iterable object used to receive BLE state event in a streaming manner.

async __aiter__() BLEConnectionResult

Returns itself, allowing the instance to be used directly in an async for loop.

async __anext__() dict

Asynchronously retrieves the next state event from the global state_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

connectionState

str

Event Type

connected disconnected

rssi

int

RSSI

-65

handle

str

Device MAC

20:00:00:00:00:01

reason

str

Disconnect reason(disconnected valid)

host disconnect

packetLossRate

int

Packet loss rate(disconnected valid)

3

Note