Scan for BLE devices
Scan BLE devices via the API, supporting custom scan parameters.
Example
1import asyncio
2import cassiablue
3
4
5async def main():
6 print("start scan")
7 ok, _ = await cassiablue.start_scan("active=1&filter_duplicates=1000")
8 print(type(ok), ok)
9 if not ok:
10 return
11
12 async for dev in cassiablue.scan_result():
13 print(type(dev), dev)
14 await asyncio.sleep_ms(5)
15
16
17asyncio.run(main())
API
- async cassiablue.start_scan(query: str | None = None) tuple[ok: bool, result: str]
Start scanning to detect nearby BLE devices. Currently, only one scan can be active at a time.
- Parameters:
query: scan parameters, refer to Scan bluetooth devices
- Returns:
ok: Whether the operation was successful
result: Execution result. For scan data, refer to
BLEScanResult
- async cassiablue.scan_result() BLEScanResult
Retrieve scan data
- Returns:
class BLEScanResult
- class BLEScanResult
An asynchronous iterable object used to receive BLE scan data in a streaming manner.
- async __aiter__() BLEScanResult
Returns itself, allowing the instance to be used directly in an
async for
loop.
- async __anext__() dict
Asynchronously retrieves the next scan result from the global
scan_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
bdaddr
str
Device MAC address
20:00:00:00:00:02
bdaddrType
str
Device address type
random
rssi
int
RSSI
-65
evtType
int
Event Type
3
name
str
Advertising name
M2000
adData
str
Advertising packet
0201060D…
scanData
str
Scan response packet
11094D…