bleak.backends.android package
Submodules
bleak.backends.android.adapter module
- class bleak.backends.android.adapter.BleakAdapterAndroid(manager: android.bluetooth.BluetoothManager)[source]
Bases:
BaseBleakAdapterThe Android Bleak BLE Adapter using Chaquopy/BeeWare.
- async classmethod get(**kwargs: Any) Self[source]
Get a Bluetooth adapter for the current platform.
- Returns:
A platform-specific
BaseBleakAdapterinstance.- Raises:
NotImplementedError – if the current backend does not support this.
- async get_connected_devices(service_uuids: frozenset[str]) list[BLEDevice][source]
Retrieve BLE devices that are currently connected to the system.
- Parameters:
service_uuids – Service UUIDs to filter on.
- Returns:
A list of
BLEDevicefor each connected BLE device.- Raises:
NotImplementedError – if the current backend does not support this.
bleak.backends.android.broadcast module
bleak.backends.android.client module
- class bleak.backends.android.client.BleakClientAndroid(address_or_ble_device: BLEDevice | str, services: set[UUID] | None, *, disconnected_callback: Callable[[], None] | None, timeout: float, **kwargs: Any)[source]
Bases:
BaseBleakClientAndroid Bleak Client using Chaquopy/BeeWare.
- Parameters:
address_or_ble_device – The Bluetooth address of the BLE peripheral to connect to or the
BLEDeviceobject representing it.services – Optional set of services UUIDs to filter.
- property is_connected: bool
Check connection status between this client and the server.
- Returns:
Boolean representing connection status.
- property mtu_size: int
Gets the negotiated MTU.
- property name: str
- async read_gatt_char(characteristic: BleakGATTCharacteristic, *, use_cached: bool = False, **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, *, use_cached: bool = False, **kwargs: Any) bytearray[source]
Perform read operation on the specified GATT descriptor.
- Parameters:
descriptor – The descriptor to read from.
use_cached – Whether to use cached value.
- Returns:
The read data.
- async start_notify(characteristic: BleakGATTCharacteristic, callback: Callable[[bytearray], None], **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:
characteristic (BleakGATTCharacteristic) – The characteristic to deactivate notification/indication on,.
- async write_gatt_char(characteristic: BleakGATTCharacteristic, data: SizedBuffer, 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: SizedBuffer) None[source]
Perform a write operation on the specified GATT descriptor.
- Parameters:
data (bytes or bytearray) – The data to send.
- class bleak.backends.android.client.ConnectObjects(adapter: 'BluetoothAdapter', device: 'BluetoothDevice', gatt: 'BluetoothGatt', callbacks: 'PythonBluetoothGattCallback', subscriptions: 'dict[int, NotifyCallback]')[source]
Bases:
object- adapter: android.bluetooth.BluetoothAdapter
- callbacks: PythonBluetoothGattCallback
- device: android.bluetooth.BluetoothDevice
- gatt: android.bluetooth.BluetoothGatt
- subscriptions: dict[int, Callable[[bytearray], None]]
bleak.backends.android.client_callback module
- class bleak.backends.android.client_callback.OnCharacteristicReadCallback(handle: 'int')[source]
Bases:
CallbackApi[OnCharacteristicReadResult]- handle: int
- class bleak.backends.android.client_callback.OnCharacteristicReadResult(value: 'bytes')[source]
Bases:
CallbackResult- value: bytes
- class bleak.backends.android.client_callback.OnCharacteristicWriteCallback(handle: 'int')[source]
Bases:
CallbackApi[EmptyCallbackResult]- handle: int
- class bleak.backends.android.client_callback.OnConnectionStateChangeResult(new_state: 'int')[source]
Bases:
CallbackResult- new_state: int
- class bleak.backends.android.client_callback.OnDescriptorReadCallback(uuid: 'str')[source]
Bases:
CallbackApi[OnDescriptorReadResult]- uuid: str
- class bleak.backends.android.client_callback.OnDescriptorReadResult(value: 'bytes')[source]
Bases:
CallbackResult- value: bytes
- class bleak.backends.android.client_callback.OnDescriptorWriteCallback(uuid: 'str')[source]
Bases:
CallbackApi[EmptyCallbackResult]- uuid: str
- class bleak.backends.android.client_callback.OnMtuChangedCallback[source]
Bases:
CallbackApi[OnMtuChangedResult]
- class bleak.backends.android.client_callback.OnMtuChangedResult(mtu: 'int')[source]
Bases:
CallbackResult- mtu: int
- class bleak.backends.android.client_callback.OnServicesDiscoveredCallback[source]
Bases:
CallbackApi[EmptyCallbackResult]
- class bleak.backends.android.client_callback.PythonBluetoothGattCallback(*args: Any, **kwargs: Any)[source]
Bases:
static_proxyCallback class for GattClient. PRIVATE.
- onCharacteristicWrite(gatt: android.bluetooth.BluetoothGatt, characteristic: android.bluetooth.BluetoothGattCharacteristic, status: int)
- onConnectionStateChange(gatt: android.bluetooth.BluetoothGatt, status: int, newState: int)
- onDescriptorWrite(gatt: android.bluetooth.BluetoothGatt, descriptor: android.bluetooth.BluetoothGattDescriptor, status: int)
- onMtuChanged(gatt: android.bluetooth.BluetoothGatt, mtu: int, status: int)
- onServicesDiscovered(gatt: android.bluetooth.BluetoothGatt, status: int)
bleak.backends.android.dispatcher module
- class bleak.backends.android.dispatcher.CallbackDispatcher(loop: AbstractEventLoop)[source]
Bases:
object- dispatch(dispatch_func: Callable[[], T], callback_api: CallbackApi[CallbackResultT]) tuple[T, Future[CallbackResultT]][source]
Register the callback future and invoke dispatch_func synchronously.
Returns the future to await for the callback result (and optionally the dispatch result as the first element of a tuple if return_dispatch_result=True). Prefer
perform_and_wait()for the common case. Use this method directly when you need the future even if the await fails (e.g. for cleanup on timeout).The dispatch_func must raise
BleakErroron failure.
- fail_all_threadsafe(exception: Exception) None[source]
Fails all pending futures with exception.
This is needed when Android won’t call the callbacks anymore, e.g. after the device disconnected.
- futures: dict[CallbackApi[Any], Future[Any]]
- async perform_and_wait(dispatch_func: Callable[[], T], callback_api: CallbackApi[CallbackResultT]) tuple[T, CallbackResultT][source]
Perform an API call and wait for the callback result.
- Parameters:
dispatch_func – The API function to call, that triggers the callback. Must raise
BleakErroron failure.callback_api – The callback to wait for to get the result.
- Returns:
Tuple of (dispatch result, callback result).
- result_state_threadsafe(failure: Exception | None, callback_api: CallbackApi[CallbackResultT], callback_result: CallbackResultT)[source]
- states: dict[CallbackApi[Any], CallbackState]
- class bleak.backends.android.dispatcher.CallbackState(failure: 'Exception | None', callback_result: 'CallbackResult')[source]
Bases:
object- callback_result: CallbackResult
- failure: Exception | None
- class bleak.backends.android.dispatcher.EmptyCallbackResult[source]
Bases:
CallbackResult
bleak.backends.android.permissions module
bleak.backends.android.scanner module
- class bleak.backends.android.scanner.BleakScannerAndroid(detection_callback: Callable[[BLEDevice, AdvertisementData], Coroutine[Any, Any, None] | None] | None, service_uuids: list[str] | None, scanning_mode: Literal['active', 'passive'], **kwargs: Any)[source]
Bases:
BaseBleakScannerAndroid Bleak BLE Scanner using Chaquopy/BeeWare.
- 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. Specifying this also enables scanning while the screen is off on Android.
scanning_mode – Set to
"passive"to avoid the"active"scanning mode.
- class bleak.backends.android.scanner.ExcessiveUsageChecker[source]
Bases:
objectOn Android, no more than 5 start/stop scanning operations are allowed per 30 seconds!
Before API level 33, Android does not report an error when this limit is exceeded, scanning just silently doesn’t work. This is a helper class to track scan start times so that we can raise an error instead.
See this commit: https://android-review.googlesource.com/c/platform/packages/apps/Bluetooth/+/215844 Or this comment: https://github.com/NordicSemiconductor/Android-Scanner-Compat-Library/issues/18#issuecomment-402412139
bleak.backends.android.scanner_callback module
- class bleak.backends.android.scanner_callback.OnScanCallback[source]
Bases:
CallbackApi[OnScanResult]
- class bleak.backends.android.scanner_callback.OnScanResult(result: "'None | ScanResult'")[source]
Bases:
CallbackResult- result: None | ScanResult
- class bleak.backends.android.scanner_callback.PythonScanCallback(*args: Any, **kwargs: Any)[source]
Bases:
static_proxyCallback class for LE Scan. PRIVATE.
This class holds methods that receive and handle data from Android’s BluetoothLeScanner methods. It is not intended to call this class directly.
- onScanFailed(errorCode: int)
- onScanResult(callbackType: int, result: android.bluetooth.le.ScanResult)