Utilities

This section shows general objects and helper functions that are used with this package.

Messages

Gatt Messages

class balderhub.ble.lib.utils.BaseGattMessage(raw_data: bytearray)

Bases: ABC

Base Gatt Message class that is used for all GATT messages within this project

classmethod from_bytearray(raw_data: bytearray) BaseGattMessage

Creates the GATT message from a bytearray

Parameters:

raw_data – the raw bytes that describe the GATT message

Returns:

a new GATT message of own type

abstract property max_allowed_length_bytes: int | None

max allowed message length in bytes (>=min_allowed_length_bytes) (None means no limit)

abstract property min_allowed_length_bytes: int

min allowed message length in bytes (<=min_allowed_length_bytes)

property raw_data: bytearray

returns a copy of the raw byte data

class balderhub.ble.lib.utils.RawGattMessage(raw_data: bytearray)

Bases: BaseGattMessage

Raw GATT message without any interpretation

max_allowed_length_bytes = None

Characteristics

class balderhub.ble.lib.utils.Characteristic(uuid: ~uuid.UUID, name: str, optional: bool = False, mandatory_properties: list[balderhub.ble.lib.utils.characteristic_property.CharacteristicProperty] = <factory>, optional_properties: list[balderhub.ble.lib.utils.characteristic_property.CharacteristicProperty] = <factory>, security: list[balderhub.ble.lib.utils.characteristic_security.CharacteristicSecurity] = CharacteristicSecurity.NONE, message_type: type[balderhub.ble.lib.utils.base_gatt_message.BaseGattMessage] = <class 'balderhub.ble.lib.utils.raw_gatt_message.RawGattMessage'>)

Bases: object

Base data class for describing a BLE GATT characteristic

message_type

alias of RawGattMessage

Characteristic Utilities

class balderhub.ble.lib.utils.CharacteristicProperty(value)

Bases: Enum

enum with all properties a characteristic can have

class balderhub.ble.lib.utils.CharacteristicSecurity(value)

Bases: Enum

enumeration to describe the security of a characteristic

Data Objects

class balderhub.ble.lib.utils.BLEDeviceInformation(address: str, name: str)

Bases: object

provides general information about a Bluetooth Low-Energy device

address: str

the address of the device

name: str

the name of the device

class balderhub.ble.lib.utils.PnpIdData(vendor_id_source: bytes, vendor_id: bytes, product_id: bytes, product_version: tuple[int, int, int])

Bases: object

PNP ID Data object

Further Utilities

class balderhub.ble.lib.utils.AsyncManagerThread

Bases: object

Base AsyncManagerThread that is used to manage the async Bleak library within a sync context that is required for Balder.

This thread can be created multiple times within one instance of this object. Nevertheless it needs to be shut down before it can be started again.

is_alive()
Returns:

returns true if the thread is alive, False otherwise

run_in_async_thread_and_wait_for_result(async_callback, *args) Any

Helper method to run any async callback within the special async manager thread :param async_callback: the async callback to run :param args: all arguments to pass to the async callback :return: the result of the async callback

start() None

This method starts the async manager thread. It raises an ValueError in case the thread is already running.

stop_and_wait_for_thread(timeout: float = 10) None

This method stops the async manager thread and waits for its termination.

Parameters:

timeout – maximum time in seconds to wait for thread