bleak.backends.corebluetooth package

Submodules

bleak.backends.corebluetooth.CentralManagerDelegate module

CentralManagerDelegate will implement the CBCentralManagerDelegate protocol to manage CoreBluetooth services and resources on the Central End

class bleak.backends.corebluetooth.CentralManagerDelegate.CentralManagerDelegate(*args: Any, **kwargs: Any)[source]

Bases: NSObject

macOS conforming python class for managing the CentralManger for BLE

centralManagerDidUpdateState_(centralManager: CoreBluetooth.CBCentralManager) None[source]
centralManager_didConnectPeripheral_(central: CoreBluetooth.CBCentralManager, peripheral: CoreBluetooth.CBPeripheral) None[source]
centralManager_didDisconnectPeripheral_error_(central: CoreBluetooth.CBCentralManager, peripheral: CoreBluetooth.CBPeripheral, error: Foundation.NSError | None) None[source]
centralManager_didDiscoverPeripheral_advertisementData_RSSI_(central: CoreBluetooth.CBCentralManager, peripheral: CoreBluetooth.CBPeripheral, advertisementData: Foundation.NSDictionary, RSSI: int) None[source]
centralManager_didFailToConnectPeripheral_error_(centralManager: CoreBluetooth.CBCentralManager, peripheral: CoreBluetooth.CBPeripheral, error: Foundation.NSError | None) None[source]
connect(peripheral: CoreBluetooth.CBPeripheral, disconnect_callback: Callable[[], None], timeout: float = 10.0) None
did_connect_peripheral(central: CoreBluetooth.CBCentralManager, peripheral: CoreBluetooth.CBPeripheral) None
did_disconnect_peripheral(central: CoreBluetooth.CBCentralManager, peripheral: CoreBluetooth.CBPeripheral, error: Foundation.NSError | None) None
did_discover_peripheral(central: CoreBluetooth.CBCentralManager, peripheral: CoreBluetooth.CBPeripheral, advertisementData: Foundation.NSDictionary, RSSI: int) None
did_fail_to_connect_peripheral(centralManager: CoreBluetooth.CBCentralManager, peripheral: CoreBluetooth.CBPeripheral, error: Foundation.NSError | None) None
disconnect(peripheral: CoreBluetooth.CBPeripheral) None
init() Self | None[source]

macOS init function for NSObject

observeValueForKeyPath_ofObject_change_context_(keyPath: str, object: Any, change: Foundation.NSDictionary, context: int) None[source]
start_scan(service_uuids: list[str] | None) None
stop_scan() None

bleak.backends.corebluetooth.PeripheralDelegate module

PeripheralDelegate

Created by kevincar <kevincarrolldavis@gmail.com>

class bleak.backends.corebluetooth.PeripheralDelegate.PeripheralDelegate(*args: Any, **kwargs: Any)[source]

Bases: NSObject

macOS conforming python class for managing the PeripheralDelegate for BLE

did_discover_characteristics_for_service(peripheral: CoreBluetooth.CBPeripheral, service: CoreBluetooth.CBService, characteristics: Foundation.NSArray.CoreBluetooth.CBCharacteristic, error: Foundation.NSError | None) None
did_discover_descriptors_for_characteristic(peripheral: CoreBluetooth.CBPeripheral, characteristic: CoreBluetooth.CBCharacteristic, error: Foundation.NSError | None) None
did_discover_services(peripheral: CoreBluetooth.CBPeripheral, services: Foundation.NSArray.CoreBluetooth.CBService, error: Foundation.NSError | None) None
did_modify_services(peripheral: CoreBluetooth.CBPeripheral, invalidated_services: Foundation.NSArray.CoreBluetooth.CBService) None
did_read_rssi(peripheral: CoreBluetooth.CBPeripheral, rssi: int, error: Foundation.NSError | None) None
did_update_name(peripheral: CoreBluetooth.CBPeripheral, name: str) None
did_update_notification_for_characteristic(peripheral: CoreBluetooth.CBPeripheral, characteristic: CoreBluetooth.CBCharacteristic, error: Foundation.NSError | None) None
did_update_value_for_characteristic(peripheral: CoreBluetooth.CBPeripheral, characteristic: CoreBluetooth.CBCharacteristic, value: Foundation.NSData | None, error: Foundation.NSError | None) None
did_update_value_for_descriptor(peripheral: CoreBluetooth.CBPeripheral, descriptor: CoreBluetooth.CBDescriptor, value: Any | None, error: Foundation.NSError | None) None
did_write_value_for_characteristic(peripheral: CoreBluetooth.CBPeripheral, characteristic: CoreBluetooth.CBCharacteristic, error: Foundation.NSError | None) None
did_write_value_for_descriptor(peripheral: CoreBluetooth.CBPeripheral, descriptor: CoreBluetooth.CBDescriptor, error: Foundation.NSError | None) None
discover_characteristics(service: CoreBluetooth.CBService) Foundation.NSArray.CoreBluetooth.CBCharacteristic
discover_descriptors(characteristic: CoreBluetooth.CBCharacteristic) Foundation.NSArray.CoreBluetooth.CBDescriptor
discover_services(services: Foundation.NSArray.CoreBluetooth.CBUUID | None = None) Foundation.NSArray.CoreBluetooth.CBService
futures() Iterable[Future[Any]]

Gets all futures for this delegate.

These can be used to handle any pending futures when a peripheral is disconnected.

initWithPeripheral_(peripheral: CoreBluetooth.CBPeripheral) PeripheralDelegate | None[source]

macOS init function for NSObject

peripheralDidUpdateName_(peripheral: CoreBluetooth.CBPeripheral) None[source]
peripheral_didDiscoverCharacteristicsForService_error_(peripheral: CoreBluetooth.CBPeripheral, service: CoreBluetooth.CBService, error: Foundation.NSError | None) None[source]
peripheral_didDiscoverDescriptorsForCharacteristic_error_(peripheral: CoreBluetooth.CBPeripheral, characteristic: CoreBluetooth.CBCharacteristic, error: Foundation.NSError | None) None[source]
peripheral_didDiscoverServices_(peripheral: CoreBluetooth.CBPeripheral, error: Foundation.NSError | None) None[source]
peripheral_didModifyServices_(peripheral: CoreBluetooth.CBPeripheral, invalidatedServices: Foundation.NSArray.CoreBluetooth.CBService) None[source]
peripheral_didReadRSSI_error_(peripheral: CoreBluetooth.CBPeripheral, rssi: int, error: Foundation.NSError | None) None[source]
peripheral_didUpdateNotificationStateForCharacteristic_error_(peripheral: CoreBluetooth.CBPeripheral, characteristic: CoreBluetooth.CBCharacteristic, error: Foundation.NSError | None) None[source]
peripheral_didUpdateValueForCharacteristic_error_(peripheral: CoreBluetooth.CBPeripheral, characteristic: CoreBluetooth.CBCharacteristic, error: Foundation.NSError | None) None[source]
peripheral_didUpdateValueForDescriptor_error_(peripheral: CoreBluetooth.CBPeripheral, descriptor: CoreBluetooth.CBDescriptor, error: Foundation.NSError | None) None[source]
peripheral_didWriteValueForCharacteristic_error_(peripheral: CoreBluetooth.CBPeripheral, characteristic: CoreBluetooth.CBCharacteristic, error: Foundation.NSError | None) None[source]
peripheral_didWriteValueForDescriptor_error_(peripheral: CoreBluetooth.CBPeripheral, descriptor: CoreBluetooth.CBDescriptor, error: Foundation.NSError | None) None[source]
read_characteristic(characteristic: CoreBluetooth.CBCharacteristic, use_cached: bool, timeout: int = 20) Foundation.NSData
read_descriptor(descriptor: CoreBluetooth.CBDescriptor, use_cached: bool = True) Any
read_rssi() int
start_notifications(characteristic: CoreBluetooth.CBCharacteristic, callback: Callable[[bytearray], None], notification_discriminator: Callable[[bytes], bool] | None = None) None
stop_notifications(characteristic: CoreBluetooth.CBCharacteristic) None
write_characteristic(characteristic: CoreBluetooth.CBCharacteristic, value: Foundation.NSData, response: CoreBluetooth.CBCharacteristicWriteType) None
write_descriptor(descriptor: CoreBluetooth.CBDescriptor, value: Foundation.NSData) None

bleak.backends.corebluetooth.client module

BLE Client for CoreBluetooth on macOS

class bleak.backends.corebluetooth.client.BleakClientCoreBluetooth(address_or_ble_device: BLEDevice | str, services: set[str] | None = None, **kwargs: Any)[source]

Bases: BaseBleakClient

CoreBluetooth class interface for BleakClient

Parameters:
  • address_or_ble_device (BLEDevice or str) – The Bluetooth address of the BLE peripheral to connect to or the BLEDevice object representing it.

  • services – Optional set of service UUIDs that will be used.

Keyword Arguments:

timeout (float) – Timeout for required BleakScanner.find_device_by_address call. Defaults to 10.0.

async connect(pair: bool, **kwargs: Any) None[source]

Connect to a specified Peripheral

Keyword Arguments:

timeout (float) – Timeout for required BleakScanner.find_device_by_address call. Defaults to 10.0.

async disconnect() None[source]

Disconnect from the peripheral device

async get_rssi() int[source]

To get RSSI value in dBm of the connected Peripheral

property is_connected: bool

Checks for current active connection

property mtu_size: int

Get ATT MTU size for active connection

async pair(*args: Any, **kwargs: Any) None[source]

Attempt to pair with a peripheral.

Raises:

NotImplementedError – This is not available on macOS since there is not explicit API to do a pairing. Instead, the docs state that it “auto-pairs”, when trying to read a characteristic that requires encryption.

Reference:

async read_gatt_char(characteristic: BleakGATTCharacteristic, **kwargs: Any) bytearray[source]

Perform read operation on the specified GATT characteristic.

Parameters:

characteristic (BleakGATTCharacteristic) – The characteristic to read from.

Returns:

(bytearray) The read data.

async read_gatt_descriptor(descriptor: BleakGATTDescriptor, **kwargs: Any) bytearray[source]

Perform read operation on the specified GATT descriptor.

Parameters:
  • handle (int) – The handle of the descriptor to read from.

  • use_cached (bool) – False forces Windows to read the value from the device again and not use its own cached value. Defaults to False.

Returns:

(bytearray) The read data.

async start_notify(characteristic: BleakGATTCharacteristic, callback: Callable[[bytearray], None], *, cb: CBStartNotifyArgs, **kwargs: Any) None[source]

Activate notifications/indications on a characteristic.

async stop_notify(characteristic: BleakGATTCharacteristic) None[source]

Deactivate notification/indication on a specified characteristic.

Parameters:

(BleakGATTCharacteristic (characteristic) – The characteristic to deactivate notification/indication on.

async unpair() None[source]

Remove pairing information for a peripheral.

Raises:

NotImplementedError – This is not available on macOS since there is not explicit API to do a pairing.

async write_gatt_char(characteristic: BleakGATTCharacteristic, data: Buffer, response: bool) None[source]

Perform a write operation on the specified GATT characteristic.

Parameters:
  • characteristic – The characteristic to write to.

  • data – The data to send.

  • response – If write-with-response operation should be done.

async write_gatt_descriptor(descriptor: BleakGATTDescriptor, data: Buffer) None[source]

Perform a write operation on the specified GATT descriptor.

Parameters:
  • descriptor – The descriptor to read from.

  • data – The data to send (any bytes-like object).

bleak.backends.corebluetooth.scanner module

class bleak.backends.corebluetooth.scanner.BleakScannerCoreBluetooth(detection_callback: Callable[[BLEDevice, AdvertisementData], Coroutine[Any, Any, None] | None] | None, service_uuids: list[str] | None, scanning_mode: Literal['active', 'passive'], *, cb: CBScannerArgs, **kwargs: Any)[source]

Bases: BaseBleakScanner

The native macOS Bleak BLE Scanner.

Documentation: https://developer.apple.com/documentation/corebluetooth/cbcentralmanager

CoreBluetooth doesn’t explicitly use Bluetooth addresses to identify peripheral devices because private devices may obscure their Bluetooth addresses. To cope with this, CoreBluetooth utilizes UUIDs for each peripheral. Bleak uses this for the BLEDevice address on macOS.

Parameters:
  • detection_callback – Optional function that will be called each time a device is discovered or advertising data has changed.

  • service_uuids – Optional list of service UUIDs to filter on. Only advertisements containing this advertising data will be received. Required on macOS >= 12.0, < 12.3 (unless you create an app with py2app).

  • scanning_mode – Set to "passive" to avoid the "active" scanning mode. Not supported on macOS! Will raise BleakError if set to "passive"

  • **timeout (float) – The scanning timeout to be used, in case of missing stopScan_ method.

async start() None[source]

Start scanning for devices

async stop() None[source]

Stop scanning for devices

bleak.backends.corebluetooth.utils module

bleak.backends.corebluetooth.utils.cb_uuid_to_str(uuid: CoreBluetooth.CBUUID) str[source]

Converts a CoreBluetooth UUID to a Python string.

If uuid is a 16-bit UUID, it is assumed to be a Bluetooth GATT UUID (0000xxxx-0000-1000-8000-00805f9b34fb).

Args

uuid: The UUID.

Returns:

The UUID as a lower case Python string (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx)

Module contents

__init__.py