Import
Classes
| Class | Description |
|---|---|
Central | BLE central role for scanning and initiating connections |
Client | BLE client for GATT operations on a connected device |
Functions
| Function | Description |
|---|---|
do_nothing() | No-op notification handler |
notify_handler() | Event-based notification handler |
waiter() | Async wait helper |
Central Class
TheCentral class provides BLE scanning and connection initiation.
Central(loop=None)
Create a BLE central instance.
| Parameter | Type | Description |
|---|---|---|
loop | asyncio.EventLoop | Event loop (optional, uses default) |
scan(scan_time=5.0, name=None, address=None)
Scan for nearby BLE devices.
| Parameter | Type | Description |
|---|---|---|
scan_time | float | Scan duration in seconds (default: 5.0) |
name | str | Filter by device name (optional) |
address | str | Filter by MAC address (optional) |
list - List of discovered BLE devices
connect(address)
Connect to a BLE device by address.
| Parameter | Type | Description |
|---|---|---|
address | str | MAC address of the device |
Client - Connected BLE client
pair(address)
Pair with a BLE device.
Client Class
TheClient class provides GATT operations on a connected BLE device.
Creating a Client
connect()
Establish connection to the BLE device.
disconnect()
Disconnect from the BLE device.
pair()
Pair with the connected device.
get_services()
Discover and retrieve all GATT services.
BleakGATTServiceCollection - Collection of discovered services
has_characteristic(uuid)
Check if a characteristic exists on the device.
| Parameter | Type | Description |
|---|---|---|
uuid | str | Characteristic UUID |
bool - True if characteristic exists
read_gatt_char(char_specifier)
Read a characteristic value.
| Parameter | Type | Description |
|---|---|---|
char_specifier | str or int | UUID string or handle number |
bytearray - Characteristic value
write_gatt_char(char_specifier, data)
Write a value to a characteristic.
| Parameter | Type | Description |
|---|---|---|
char_specifier | str or int | UUID string or handle number |
data | bytes | Data to write |
start_notify(char_specifier, callback=do_nothing, max_messages=None, timeout=None)
Subscribe to characteristic notifications.
| Parameter | Type | Description |
|---|---|---|
char_specifier | str or int | Characteristic UUID or handle |
callback | callable | Function called for each notification |
max_messages | int | Stop after receiving this many messages |
timeout | float | Timeout in seconds |
tuple[bool, list] - (timed_out, messages) where timed_out is True if timeout occurred
stop_notify(char_specifier)
Unsubscribe from characteristic notifications.
sleep(timeout)
Sleep for a duration (async-safe).
Helper Functions
do_nothing(handle, data)
A no-op notification handler.
notify_handler(evt, messages, callback, max_messages, handle, data)
Internal notification handler that collects messages and signals completion.
waiter(event, timeout)
Async wait helper for notification events.
Examples
Scan for Devices
Connect and Read Characteristic
Subscribe to Notifications
Write Command and Read Response
Device Firmware Version Check
BLE Production Test
Hardware Requirements
| Requirement | Description |
|---|---|
| BLE Hardware | Bluetooth 4.0+ adapter on gateway |
| Permissions | May require root/sudo for BLE operations |
Dependencies
The BLE module uses Bleak as the underlying BLE library, which provides cross-platform BLE support.Notes
- BLE operations are synchronous wrappers around async Bleak operations
- The Client class supports context manager (
withstatement) for automatic cleanup - Notification callbacks receive
(handle, data)parameters - Use
max_messagesandtimeouttogether to control notification collection - MAC addresses are typically in format
AA:BB:CC:DD:EE:FF - Some BLE operations may require pairing before they work
- Signal strength (RSSI) is available on scanned device objects

