[Python] Python Bluetooth advertising fails when I include any manufacturing data

Discussão em 'Python' iniciado por Stack, Junho 28, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using a Raspberry Pi 3 with a fresh install of Raspberry Pi OS Lite 32-bit. I have a Bluetooth Dongle that supports Bluetooth Low Energy. I'm running Python 3.11.2.

    I am attempting to broadcast a Bluetooth advertisement. When I run my Python script I can see the advertisement using the LightBlue or nRF Connect phone apps. However, as soon as I add manufacturing data, I can no longer see the advertisement.

    Here is my Python code:

    import dbus
    import dbus.mainloop.glib
    import dbus.service
    from gi.repository import GLib

    MANUFACTURER_ID = 0x0397 # LEGO
    PAYLOAD_BYTES = b'\x10\x06\x18\x1a\xb6'

    class Advertisement(dbus.service.Object):
    PATH_BASE = '/org/bluez/example/advertisement'

    def __init__(self, bus, index):
    self.path = self.PATH_BASE + str(index)
    self.bus = bus
    super().__init__(bus, self.path)

    def get_path(self):
    return dbus.ObjectPath(self.path)

    def get_properties(self):
    return {
    'org.bluez.LEAdvertisement1': {
    'Type': 'peripheral',
    'LocalName': 'MyBrickPi',
    '''
    'ManufacturerData': {
    # dbus.Array([dbus.Byte(0x00)], signature='y')
    # dbus.UInt16(0x0397): dbus.Array([dbus.Byte(0x01)], signature='y')
    # dbus.UInt16(0x0397): dbus.Array([dbus.Byte(b) for b in b'\x10\x06\x18\x1a\xb6'], signature='y')
    # dbus.UInt16(0xFFFF): dbus.Array([dbus.Byte(0x01)], signature='y')
    # dbus.UInt16(0x0000): dbus.Array([dbus.Byte(0x01)], signature='y')
    # {dbus.UInt16(0x0000): [16, 5, 7, 24, 186, 175, 161]}
    # [{16: [[b'4a04389']]}]
    },
    '''
    # 'IncludeTxPower': True,
    # 'Appearance': dbus.UInt16(0),
    }
    }

    @dbus.service.method('org.freedesktop.DBus.Properties',
    in_signature='s',
    out_signature='a{sv}')
    def GetAll(self, interface):
    return self.get_properties()['org.bluez.LEAdvertisement1']

    @dbus.service.method('org.bluez.LEAdvertisement1',
    in_signature='',
    out_signature='')
    def Release(self):
    print('Advertisement released')


    def main():
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    bus = dbus.SystemBus()

    adapter_path = '/org/bluez/hci0'
    adapter = bus.get_object('org.bluez', adapter_path)
    adapter_props = dbus.Interface(adapter, 'org.freedesktop.DBus.Properties')

    # Power on adapter if off
    try:
    powered = adapter_props.Get('org.bluez.Adapter1', 'Powered')
    if not powered:
    adapter_props.Set('org.bluez.Adapter1', 'Powered', dbus.Boolean(1))
    except Exception as e:
    print(f"Failed to get/set adapter power state: {e}")
    return

    ad_manager = dbus.Interface(adapter, 'org.bluez.LEAdvertisingManager1')

    advertisement = Advertisement(bus, 0)

    def on_success():
    print("Advertising started.")

    def on_error(error):
    print(f"Failed to advertise: {error}")

    ad_manager.RegisterAdvertisement(advertisement.get_path(), {},
    reply_handler=on_success,
    error_handler=on_error)

    try:
    GLib.MainLoop().run()
    except KeyboardInterrupt:
    print("\nStopping advertisement...")
    try:
    ad_manager.UnregisterAdvertisement(advertisement)
    except Exception:
    print("Advertisement was not registered or already unregistered.")
    advertisement.remove_from_connection()


    if __name__ == '__main__':
    main()


    The get_properties function returns the advertising data. Currently the ManufacturerData is commented out. As soon as I put that back in, there are no errors, but I can not detect the advertisement using LightBlue or nRF Connect. I left in a number of different formats for the manufacturing data that I have tried.

    I commented out the IncludeTxPower and Appearance, but they don't seem to have an effect either way.

    Eventually I want the manufacture ID to be 919 (LEGO). And at this point I don't care what the second data is, just any ASCII string. I just want to get something working.

    Continue reading...

Compartilhe esta Página