bleak.backends package
Subpackages
- bleak.backends.bluezdbus package
- Submodules
- bleak.backends.bluezdbus.advertisement_monitor module
- bleak.backends.bluezdbus.characteristic module
- bleak.backends.bluezdbus.client module
- bleak.backends.bluezdbus.defs module
- bleak.backends.bluezdbus.descriptor module
- bleak.backends.bluezdbus.manager module
- bleak.backends.bluezdbus.scanner module
- bleak.backends.bluezdbus.service module
- bleak.backends.bluezdbus.signals module
- bleak.backends.bluezdbus.utils module
- bleak.backends.bluezdbus.version module
- Module contents
- bleak.backends.corebluetooth package
- Submodules
- bleak.backends.corebluetooth.CentralManagerDelegate module
- bleak.backends.corebluetooth.PeripheralDelegate module
- bleak.backends.corebluetooth.characteristic module
- bleak.backends.corebluetooth.client module
- bleak.backends.corebluetooth.descriptor module
- bleak.backends.corebluetooth.device module
- bleak.backends.corebluetooth.scanner module
- bleak.backends.corebluetooth.service module
- bleak.backends.corebluetooth.utils module
- Module contents
- bleak.backends.p4android package
- Submodules
- bleak.backends.p4android.characteristic module
- bleak.backends.p4android.client module
- bleak.backends.p4android.defs module
- bleak.backends.p4android.descriptor module
- bleak.backends.p4android.scanner module
- bleak.backends.p4android.service module
- bleak.backends.p4android.utils module
- Module contents
- bleak.backends.winrt package
Submodules
bleak.backends.characteristic module
Interface class for the Bleak representation of a GATT Characteristic
Created on 2019-03-19 by hbldh <henrik.blidh@nedomkull.com>
- class bleak.backends.characteristic.BleakGATTCharacteristic(obj: Any, max_write_without_response_size: int)[source]
Bases:
abc.ABC
Interface for the Bleak representation of a GATT Characteristic
- abstract add_descriptor(descriptor: bleak.backends.descriptor.BleakGATTDescriptor)[source]
Add a
BleakGATTDescriptor
to the characteristic.Should not be used by end user, but rather by bleak itself.
- property description: str
Description for this characteristic
- abstract property descriptors: List
List of descriptors for this service
- abstract get_descriptor(specifier: Union[int, str, uuid.UUID]) Optional[bleak.backends.descriptor.BleakGATTDescriptor] [source]
Get a descriptor by handle (int) or UUID (str or uuid.UUID)
- abstract property handle: int
The handle for this characteristic
- property max_write_without_response_size: int
Gets the maximum size in bytes that can be used for the data argument of
BleakClient.write_gatt_char()
whenresponse=False
.
- abstract property properties: List[str]
Properties of this characteristic
- abstract property service_handle: int
The integer handle of the Service containing this characteristic
- abstract property service_uuid: str
The UUID of the Service containing this characteristic
- abstract property uuid: str
The UUID for this characteristic
- class bleak.backends.characteristic.GattCharacteristicsFlags(value)[source]
Bases:
enum.Enum
An enumeration.
- authenticated_signed_writes = 64
- broadcast = 1
- extended_properties = 128
- indicate = 32
- notify = 16
- read = 2
- reliable_write = 256
- writable_auxiliaries = 512
- write = 8
- write_without_response = 4
bleak.backends.client module
Base class for backend clients.
Created on 2018-04-23 by hbldh <henrik.blidh@nedomkull.com>
- class bleak.backends.client.BaseBleakClient(address_or_ble_device: Union[bleak.backends.device.BLEDevice, str], **kwargs)[source]
Bases:
abc.ABC
The Client Interface for Bleak Backend implementations to implement.
The documentation of this interface should thus be safe to use as a reference for your implementation.
- Parameters
address_or_ble_device (BLEDevice or str) – The Bluetooth address of the BLE peripheral to connect to or the BLEDevice object representing it.
- Keyword Arguments
timeout (float) – Timeout for required
discover
call. Defaults to 10.0.disconnected_callback (callable) – Callback that will be scheduled in the event loop when the client is disconnected. The callable must take one argument, which will be this client object.
- abstract async connect(**kwargs) bool [source]
Connect to the specified GATT server.
- Returns
Boolean representing connection status.
- abstract async disconnect() bool [source]
Disconnect from the specified GATT server.
- Returns
Boolean representing connection status.
- abstract async get_services(**kwargs) bleak.backends.service.BleakGATTServiceCollection [source]
Get all services registered for this GATT server.
- Returns
A
bleak.backends.service.BleakGATTServiceCollection
with this device’s services tree.
- abstract property is_connected: bool
Check connection status between this client and the server.
- Returns
Boolean representing connection status.
- abstract async read_gatt_char(char_specifier: Union[bleak.backends.characteristic.BleakGATTCharacteristic, int, str, uuid.UUID], **kwargs) bytearray [source]
Perform read operation on the specified GATT characteristic.
- Parameters
char_specifier (BleakGATTCharacteristic, int, str or UUID) – The characteristic to read from, specified by either integer handle, UUID or directly by the BleakGATTCharacteristic object representing it.
- Returns
(bytearray) The read data.
- abstract async read_gatt_descriptor(handle: int, **kwargs) bytearray [source]
Perform read operation on the specified GATT descriptor.
- Parameters
handle (int) – The handle of the descriptor to read from.
- Returns
(bytearray) The read data.
- set_disconnected_callback(callback: Optional[Callable[[bleak.backends.client.BaseBleakClient], None]], **kwargs) None [source]
Set the disconnect callback. The callback will only be called on unsolicited disconnect event.
Callbacks must accept one input which is the client object itself.
Set the callback to
None
to remove any existing callback.def callback(client): print("Client with address {} got disconnected!".format(client.address)) client.set_disconnected_callback(callback) client.connect()
- Parameters
callback – callback to be called on disconnection.
- abstract async start_notify(char_specifier: Union[bleak.backends.characteristic.BleakGATTCharacteristic, int, str, uuid.UUID], callback: Callable[[int, bytearray], None], **kwargs) None [source]
Activate notifications/indications on a characteristic.
Callbacks must accept two inputs. The first will be a integer handle of the characteristic generating the data and the second will be a
bytearray
.def callback(sender: int, data: bytearray): print(f"{sender}: {data}") client.start_notify(char_uuid, callback)
- Parameters
char_specifier (BleakGATTCharacteristic, int, str or UUID) – The characteristic to activate notifications/indications on a characteristic, specified by either integer handle, UUID or directly by the BleakGATTCharacteristic object representing it.
callback (function) – The function to be called on notification.
- abstract async stop_notify(char_specifier: Union[bleak.backends.characteristic.BleakGATTCharacteristic, int, str, uuid.UUID]) None [source]
Deactivate notification/indication on a specified characteristic.
- Parameters
char_specifier (BleakGATTCharacteristic, int, str or UUID) – The characteristic to deactivate notification/indication on, specified by either integer handle, UUID or directly by the BleakGATTCharacteristic object representing it.
- abstract async write_gatt_char(char_specifier: Union[bleak.backends.characteristic.BleakGATTCharacteristic, int, str, uuid.UUID], data: Union[bytes, bytearray, memoryview], response: bool = False) None [source]
Perform a write operation on the specified GATT characteristic.
- Parameters
char_specifier (BleakGATTCharacteristic, int, str or UUID) – The characteristic to write to, specified by either integer handle, UUID or directly by the BleakGATTCharacteristic object representing it.
data (bytes or bytearray) – The data to send.
response (bool) – If write-with-response operation should be done. Defaults to False.
bleak.backends.descriptor module
Interface class for the Bleak representation of a GATT Descriptor
Created on 2019-03-19 by hbldh <henrik.blidh@nedomkull.com>
- class bleak.backends.descriptor.BleakGATTDescriptor(obj: Any)[source]
Bases:
abc.ABC
Interface for the Bleak representation of a GATT Descriptor
- abstract property characteristic_handle: int
handle for the characteristic that this descriptor belongs to
- abstract property characteristic_uuid: str
UUID for the characteristic that this descriptor belongs to
- property description: str
A text description of what this descriptor represents
- abstract property handle: int
Integer handle for this descriptor
- abstract property uuid: str
UUID for this descriptor
bleak.backends.device module
Wrapper class for Bluetooth LE servers returned from calling
bleak.discover()
.
Created on 2018-04-23 by hbldh <henrik.blidh@nedomkull.com>
- class bleak.backends.device.BLEDevice(address, name, details=None, rssi=0, **kwargs)[source]
Bases:
object
A simple wrapper class representing a BLE server detected during a discover call.
When using Windows backend, details attribute is a
Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisement
object, unless it is created with the Windows.Devices.Enumeration discovery method, then is is aWindows.Devices.Enumeration.DeviceInformation
.When using Linux backend,
details
attribute is a dict with keyspath
which has the string path to the DBus device object andprops
which houses the properties dictionary of the D-Bus Device.When using macOS backend,
details
attribute will be a CBPeripheral object.
- address
The Bluetooth address of the device on this machine.
- details
The OS native details required for connecting to the device.
- metadata
Device specific details. Contains a
uuids
key which is a list of service UUIDs and amanufacturer_data
field with a bytes-object from the advertised data.
- name
The advertised name of the device.
- rssi
RSSI, if available
bleak.backends.scanner module
- class bleak.backends.scanner.AdvertisementData(**kwargs)[source]
Bases:
object
Wrapper around the advertisement data that each platform returns upon discovery
- class bleak.backends.scanner.BaseBleakScanner(detection_callback: Optional[Callable[[bleak.backends.device.BLEDevice, bleak.backends.scanner.AdvertisementData], Optional[Awaitable[None]]]], service_uuids: Optional[List[str]])[source]
Bases:
abc.ABC
Interface for Bleak Bluetooth LE Scanners
- 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.
- async classmethod discover(timeout=5.0, **kwargs) List[bleak.backends.device.BLEDevice] [source]
Scan continuously for
timeout
seconds and return discovered devices.- Parameters
timeout – Time to scan for.
- Keyword Arguments
**kwargs – Implementations might offer additional keyword arguments sent to the constructor of the BleakScanner class.
Returns:
- abstract property discovered_devices: List[bleak.backends.device.BLEDevice]
Gets the devices registered by the BleakScanner.
- Returns
A list of the devices that the scanner has discovered during the scanning.
- async classmethod find_device_by_address(device_identifier: str, timeout: float = 10.0, **kwargs) Optional[bleak.backends.device.BLEDevice] [source]
A convenience method for obtaining a
BLEDevice
object specified by Bluetooth address or (macOS) UUID address.- Parameters
device_identifier (str) – The Bluetooth/UUID address of the Bluetooth peripheral sought.
timeout (float) – Optional timeout to wait for detection of specified peripheral before giving up. Defaults to 10.0 seconds.
- Keyword Arguments
adapter (str) – Bluetooth adapter to use for discovery.
- Returns
The
BLEDevice
sought orNone
if not detected.
- async classmethod find_device_by_filter(filterfunc: Callable[[bleak.backends.device.BLEDevice, bleak.backends.scanner.AdvertisementData], bool], timeout: float = 10.0, **kwargs) Optional[bleak.backends.device.BLEDevice] [source]
A convenience method for obtaining a
BLEDevice
object specified by a filter function.- Parameters
filterfunc (AdvertisementDataFilter) – A function that is called for every BLEDevice found. It should return True only for the wanted device.
timeout (float) – Optional timeout to wait for detection of specified peripheral before giving up. Defaults to 10.0 seconds.
- Keyword Arguments
adapter (str) – Bluetooth adapter to use for discovery.
- Returns
The
BLEDevice
sought orNone
if not detected.
- async get_discovered_devices() List[bleak.backends.device.BLEDevice] [source]
Gets the devices registered by the BleakScanner.
Deprecated since version 0.11.0: This method will be removed in a future version of Bleak. Use the
discovered_devices
property instead.- Returns
A list of the devices that the scanner has discovered during the scanning.
- register_detection_callback(callback: Optional[Callable[[bleak.backends.device.BLEDevice, bleak.backends.scanner.AdvertisementData], Optional[Awaitable[None]]]]) None [source]
Register a callback that is called when a device is discovered or has a property changed.
If another callback has already been registered, it will be replaced with
callback
.None
can be used to remove the current callback.The
callback
is a function or coroutine that takes two arguments:BLEDevice
andAdvertisementData
.- Parameters
callback – A function, coroutine or
None
.
bleak.backends.service module
Gatt Service Collection class and interface class for the Bleak representation of a GATT Service.
Created on 2019-03-19 by hbldh <henrik.blidh@nedomkull.com>
- class bleak.backends.service.BleakGATTService(obj)[source]
Bases:
abc.ABC
Interface for the Bleak representation of a GATT Service.
- abstract add_characteristic(characteristic: bleak.backends.characteristic.BleakGATTCharacteristic)[source]
Add a
BleakGATTCharacteristic
to the service.Should not be used by end user, but rather by bleak itself.
- abstract property characteristics: List[bleak.backends.characteristic.BleakGATTCharacteristic]
List of characteristics for this service
- property description: str
String description for this service
- get_characteristic(uuid: Union[str, uuid.UUID]) Optional[bleak.backends.characteristic.BleakGATTCharacteristic] [source]
Get a characteristic by UUID.
- Parameters
uuid – The UUID to match.
- Returns
The first characteristic matching
uuid
orNone
if no matching characteristic was found.
- abstract property handle: int
The handle of this service
- abstract property uuid: str
The UUID to this service
- class bleak.backends.service.BleakGATTServiceCollection[source]
Bases:
object
Simple data container for storing the peripheral’s service complement.
- add_characteristic(characteristic: bleak.backends.characteristic.BleakGATTCharacteristic)[source]
Add a
BleakGATTCharacteristic
to the service collection.Should not be used by end user, but rather by bleak itself.
- add_descriptor(descriptor: bleak.backends.descriptor.BleakGATTDescriptor)[source]
Add a
BleakGATTDescriptor
to the service collection.Should not be used by end user, but rather by bleak itself.
- add_service(service: bleak.backends.service.BleakGATTService)[source]
Add a
BleakGATTService
to the service collection.Should not be used by end user, but rather by bleak itself.
- property characteristics: Dict[int, bleak.backends.characteristic.BleakGATTCharacteristic]
Returns dictionary of handles mapping to BleakGATTCharacteristic
- property descriptors: Dict[int, bleak.backends.descriptor.BleakGATTDescriptor]
Returns a dictionary of integer handles mapping to BleakGATTDescriptor
- get_characteristic(specifier: Union[int, str, uuid.UUID]) Optional[bleak.backends.characteristic.BleakGATTCharacteristic] [source]
Get a characteristic by handle (int) or UUID (str or uuid.UUID)
- get_descriptor(handle: int) Optional[bleak.backends.descriptor.BleakGATTDescriptor] [source]
Get a descriptor by integer handle
- get_service(specifier: Union[int, str, uuid.UUID]) Optional[bleak.backends.service.BleakGATTService] [source]
Get a service by handle (int) or UUID (str or uuid.UUID)
- property services: Dict[int, bleak.backends.service.BleakGATTService]
Returns dictionary of handles mapping to BleakGATTService
Module contents
__init__.py
Created on 2017-11-19 by hbldh <henrik.blidh@nedomkull.com>