BleakAdapter class
- class bleak.BleakAdapter(backend: BaseBleakAdapter)[source]
Interface for Bleak Bluetooth adapter operations.
Provides operations that do not require active scanning, such as retrieving devices that are already connected to the system.
To get an instance, use
adapter = await BleakAdapter.get()instead ofadapter = BleakAdapter().Added in version unreleased.
Getting an adapter
A BleakAdapter instance is created via the async
BleakAdapter.get() factory method:
import asyncio
from bleak import BleakAdapter
async def main():
adapter = await BleakAdapter.get()
...
asyncio.run(main())
On Linux, a specific adapter can be selected via the bluez argument:
adapter = await BleakAdapter.get(bluez={"adapter": "hci1"})
- async classmethod BleakAdapter.get(*, bluez: BlueZAdapterArgs = {}, backend: type[BaseBleakAdapter] | None = None) Self[source]
Get a Bluetooth adapter for the current platform.
Currently all platforms other than Linux only allow one Bluetooth adapter. On Linux, the adapter can be selected via the
bluezargument with{"adapter": "<adapter_name>"}.- Parameters:
bluez – Dictionary of arguments specific to the BlueZ backend.
backend – Used to override the automatically selected backend (i.e. for a custom backend).
- Returns:
A
BleakAdapterinstance.- Raises:
NotImplementedError – on platforms where this is not supported.
Listing connected BLE devices
The adapter can be queried for BLE devices that are already connected to the
system. The returned BLEDevice objects can be passed directly to
BleakClient to use the existing OS-level connection without scanning:
from bleak import BleakAdapter, BleakClient
adapter = await BleakAdapter.get()
devices = await adapter.get_connected_devices()
for d in devices:
print(d.name, d.address)
# Use an existing connection without scanning
async with BleakClient(devices[0]) as client:
...
- async BleakAdapter.get_connected_devices(service_uuids: list[str] = ['1801']) list[BLEDevice][source]
Retrieve BLE devices that are currently connected to the system.
This does not require starting a scan. It queries the operating system for devices that are already connected at the OS level.
The returned
BLEDeviceobjects can be passed directly toBleakClientfor getting the existing connection without scanning.- Parameters:
service_uuids – Service UUIDs to filter on. Defaults to the Generic Attribute Profile (
0x1801), which is present on essentially all BLE devices.- Returns:
A list of
BLEDeviceobjects representing connected devices.- Raises:
ValueError – if
service_uuidsis empty.