0

Currently I'm wrestling with interacting with a bluetooth module using this sdk: https://github.com/LairdCP/igsdk/blob/master/python/igsdk/bt_module.py

The intent is to use the bluetooth module to scan for all devices in range, and connect to any that start with some device name (ex. 'dev001', 'dev002', where the device would look for 'dev'). I have the beginnings of such a script, where I can scan for devices and see data coming in, I just don't know how to parse the dbus dictionary that results from each device.

Script beginning:

def discoveryCallback(x, y): 
    print("found a device!")
    print(str(x))
    print(":")
    global lastDevice
    lastDevice = y

def propertyChanged():
    print("a property has changed")

manager = bt_module.BtMgr(discoveryCallback, propertyChanged)
manager.start_discovery()

As a test, I stored the output of the scan callback in lastDevice, which has the following contents:

dbus.Dictionary({dbus.String('org.freedesktop.DBus.Introspectable'): dbus.Dictionary({}, signature=dbus.Signature('svluez.Device1'): dbus.Dictionary({dbus.String('Address'): dbus.String('4A:AA:2D:FF:48:87', variant_level=1), dbus.Stri.String('random', variant_level=1), dbus.String('Alias'): dbus.String('4A-AA-2D-FF-48-87', variant_level=1), dbus.Strlean(False, variant_level=1), dbus.String('Trusted'): dbus.Boolean(False, variant_level=1), dbus.String('Blocked'): dant_level=1), dbus.String('LegacyPairing'): dbus.Boolean(False, variant_level=1), dbus.String('RSSI'): dbus.Int16(-10us.String('Connected'): dbus.Boolean(False, variant_level=1), dbus.String('UUIDs'): dbus.Array([], signature=dbus.Sigvel=1), dbus.String('Adapter'): dbus.ObjectPath('/org/bluez/hci0', variant_level=1), dbus.String('ManufacturerData'):UInt16(76): dbus.Array([dbus.Byte(16), dbus.Byte(5), dbus.Byte(93), dbus.Byte(28), dbus.Byte(31), dbus.Byte(107), dbu=dbus.Signature('y'), variant_level=1)}, signature=dbus.Signature('qv'), variant_level=1), dbus.String('TxPower'): dbvel=1), dbus.String('ServicesResolved'): dbus.Boolean(False, variant_level=1)}, signature=dbus.Signature('sv')), dbusp.DBus.Properties'): dbus.Dictionary({}, signature=dbus.Signature('sv'))}, signature=dbus.Signature('sa{sv}'))

To connect, I need to pull the device name and MAC address from this object, but I'm stuck unable to parse it. Can anyone help?

cMod
  • 3
  • 2

1 Answers1

0

I get a SyntaxError: invalid syntax when I try to use the value for you have posted for lastDevice. Did something break in the copy and paste?

Typically these values can be treated like a python dictionary. Here is an example:

def new_iface(path, iface_props):
    """If a new dbus interfaces is a device, add it to be  monitored"""
    device_addr = iface_props.get('org.bluez.Device1', {}).get('Address')
    if device_addr:
        DeviceMonitor(path)

Which is part of the example posted at: https://stackoverflow.com/a/62917686/7721752

ukBaz
  • 6,985
  • 2
  • 8
  • 31
  • Hm, I'd like to try your linked example, but I'm not sure what iface_props would be in the context of my own code. – cMod Nov 17 '20 at 20:02
  • If you can update the question with the value of `x` and `y` from your `discoveryCallback` and I can have a go at doing the code to reading it. – ukBaz Nov 17 '20 at 22:02