Bluetooth Low Energy

class BLEConnection(char_rx_UUID, char_tx_UUID, max_data_size)

Configure BLE, connect, send data, and handle receive events.

Initializes and configures connection settings.

Parameters
  • char_rx_UUID (str) – UUID for RX.

  • char_rx_UUID – UUID for TX.

  • max_data_size (int) – Maximum number of bytes per write operation.

async connect(device: bleak.backends.device.BLEDevice)

Connects to a BLE device.

Parameters

device – Client device

data_handler(sender, data)

Handles new incoming data.

This is usually overridden by a mixin class.

Parameters
  • sender (str) – Sender uuid.

  • data (bytes) – Bytes to process.

async disconnect()

Disconnects the client from the server.

disconnected_handler(client: bleak.backends.bluezdbus.client.BleakClientBlueZDBus)

Handles disconnected event.

async write(data, with_response=False)

Write bytes to the server, split to chunks of maximum data size.

Parameters
  • data (bytearray) – Data to be sent to the server.

  • with_response (bool) – Write with or without response.

class BLERequestsConnection(UUID)

Sends messages and awaits replies of known length.

This can be used for devices with known commands and known replies, such as some bootloaders to update firmware over the air.

Initialize the BLE Connection.

data_handler(sender, data)

Handles new incoming data and raise event when a new reply is ready.

Parameters
  • sender (str) – Sender uuid.

  • data (bytes) – Bytes to process.

prepare_reply()

Clears existing reply and wait event.

This is usually called prior to the write operation, to ensure we receive some of the bytes while are still awaiting the sending process.

async wait_for_reply(timeout=None)

Awaits for given number of characters since prepare_reply.

Parameters

timeout (float or None) – Time out to await. Same as asyncio.wait_for.

Returns

The reply.

Return type

bytearray

Raises

asyncio.TimeoutError – Same as asyncio.wait_for.

async find_device(name: Optional[str] = None, service: str = 'c5f50001-8280-46da-89f4-6d8051e4aeef', timeout: float = 10)bleak.backends.device.BLEDevice

Finds a BLE device that is currently advertising that matches the given parameters.

Parameters
  • name – The device name. This can also be the Bluetooth address on non-Apple platforms or a UUID on Apple platforms. If name is None then it is not used as part of the matching criteria.

  • service – The service UUID that is advertized.

  • timeout – How long to search before giving up.

Returns

The first detected matching device.

Raises

asyncio.TimeoutError – Device was not found within the timeout.