Skip to content

Bumble Python API

Classes

Address

Bluetooth Address (see Bluetooth spec Vol 6, Part B - 1.3 DEVICE ADDRESS) NOTE: the address bytes are stored in little-endian byte order here, so address[0] is the LSB of the address, address[5] is the MSB.

__init__(self, address, address_type=1) special

Initialize an instance. address may be a byte array in little-endian format, or a hex string in big-endian format (with optional ':' separators between the bytes). If the address is a string suffixed with '/P', address_type is ignored and the type is set to PUBLIC_DEVICE_ADDRESS.

Source code in bumble/hci.py
def __init__(self, address, address_type = RANDOM_DEVICE_ADDRESS):
    '''
    Initialize an instance. `address` may be a byte array in little-endian
    format, or a hex string in big-endian format (with optional ':'
    separators between the bytes).
    If the address is a string suffixed with '/P', `address_type` is ignored and the type
    is set to PUBLIC_DEVICE_ADDRESS.
    '''
    if type(address) is bytes:
        self.address_bytes = address
    else:
        # Check if there's a '/P' type specifier
        if address.endswith('P'):
            address_type = Address.PUBLIC_DEVICE_ADDRESS
            address = address[:-2]

        if len(address) == 12 + 5:
            # Form with ':' separators
            address = address.replace(':', '')
        self.address_bytes = bytes(reversed(bytes.fromhex(address)))

    if len(self.address_bytes) != 6:
        raise ValueError('invalid address length')

    self.address_type = address_type

__str__(self) special

String representation of the address, MSB first

Source code in bumble/hci.py
def __str__(self):
    '''
    String representation of the address, MSB first
    '''
    return ':'.join([f'{x:02X}' for x in reversed(self.address_bytes)])

HCI_Packet

Abstract Base class for HCI packets

HCI Commands

HCI_Command

See Bluetooth spec @ Vol 2, Part E - 5.4.1 HCI Command Packet

command(fields=[], return_parameters_fields=[]) staticmethod

Decorator used to declare and register subclasses

Source code in bumble/hci.py
@staticmethod
def command(fields=[], return_parameters_fields=[]):
    '''
    Decorator used to declare and register subclasses
    '''

    def inner(cls):
        cls.name = cls.__name__.upper()
        cls.op_code = key_with_value(HCI_COMMAND_NAMES, cls.name)
        if cls.op_code is None:
            raise KeyError('command not found in HCI_COMMAND_NAMES')
        cls.fields = fields
        cls.return_parameters_fields = return_parameters_fields

        # Patch the __init__ method to fix the op_code
        def init(self, parameters=None, **kwargs):
            return HCI_Command.__init__(self, cls.op_code, parameters, **kwargs)
        cls.__init__ = init

        # Register a factory for this class
        HCI_Command.command_classes[cls.op_code] = cls

        return cls

    return inner

HCI_Disconnect_Command

See Bluetooth spec @ 7.1.6 Disconnect Command