bleak.BleakClient
Before doing anything else with a BleakClient
object, it must be connected.
bleak.BleakClient
is a an async context manager, so the recommended way of connecting is to use it as such:
import asyncio
from bleak import BleakClient
async def main():
async with BleakClient("XX:XX:XX:XX:XX:XX") as client:
# Read a characteristic, etc.
...
# Device will disconnect when block exits.
...
# Using asyncio.run() is important to ensure that device disconnects on
# KeyboardInterrupt or other unhandled exception.
asyncio.run(main())
It is also possible to connect and disconnect without a context manager, however this can leave the device still connected when the program exits:
bleak.BleakClient.connect
bleak.BleakClient.disconnect
The current connection status can be retrieved with:
bleak.BleakClient.is_connected
A callback can be provided to the BleakClient
constructor via the disconnect_callback
argument to be notified of disconnection events.
bleak.BleakClient.address
bleak.BleakClient.mtu_size
All Bluetooth Low Energy devices use a common Generic Attribute Profile (GATT) for interacting with the device after it is connected. Some GATT operations like discovering the services/characteristic/descriptors and negotiating the MTU are handled automatically by Bleak and/or the OS Bluetooth stack.
The primary operations for the Bleak client are reading, writing and subscribing to characteristics.
The available services on a device are automatically enumerated when connecting to a device. Services describe the devices capabilities.
bleak.BleakClient.services
Most I/O with a device is done via the characteristics.
bleak.BleakClient.read_gatt_char
bleak.BleakClient.write_gatt_char
bleak.BleakClient.start_notify
bleak.BleakClient.stop_notify
Descriptors can provide additional information about a characteristic.
bleak.BleakClient.read_gatt_descriptor
bleak.BleakClient.write_gatt_descriptor
On some devices, some characteristics may require authentication in order to read or write the characteristic. In this case pairing/bonding the device is required.
bleak.BleakClient.pair
bleak.BleakClient.unpair
bleak.BleakClient.set_disconnected_callback
bleak.BleakClient.get_services