Python Forum
QtDBus: how to call GetManagedObjects from bluez - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: GUI (https://python-forum.io/Forum-GUI)
+--- Thread: QtDBus: how to call GetManagedObjects from bluez (/Thread-QtDBus-how-to-call-GetManagedObjects-from-bluez)



QtDBus: how to call GetManagedObjects from bluez - Alfalfa - Jul-15-2018

I want to list and extract all trusted bluetooth device by using QtDbus. With the qdbusviewer tool, I can see and call the method GetManagedObjects() just fine. Then I tried to do it in python, but the method cannot be found. The script below output ['Method "GetManagedObjects" with signature "" on interface "org.freedesktop.DBus.ObjectManager" doesn\'t exist']. What am I doing wrong?

#!/usr/bin/python3
import sys
from PyQt5 import QtWidgets, QtDBus

class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        bus = QtDBus.QDBusConnection.systemBus()
        interface = QtDBus.QDBusInterface("org.bluez", "/org/bluez", "org.freedesktop.DBus.ObjectManager", bus)
        result = interface.call("GetManagedObjects")
        print(result.arguments())


if __name__== '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())



RE: QtDBus: how to call GetManagedObjects from bluez - Alfalfa - Sep-16-2018

In case anyone would like to do something similar, here is how I've done it. It list all bluetooth nodes and it's trusted devices. As read here, the trick was to use freedesktop interface to use the Get method on org.bluez.Device1.

#!/usr/bin/python3
import sys
import xml.etree.ElementTree as ET
from PyQt5 import QtCore, QtGui, QtWidgets, QtDBus

class Main(QtCore.QObject):
    def __init__(self):
        super().__init__()
        self._bluezEnumerate()

    def _bluezEnumerate(self):
        service = "org.bluez"
        root = "/org/bluez"
        interface = "org.freedesktop.DBus.Properties"
        bus = QtDBus.QDBusConnection.systemBus()
        nodes = self._bluezNodes()
        for hci in nodes:
            print(hci)
            for node in nodes[hci]:
                path = f"{root}/{hci}/{node}"
                connection = QtDBus.QDBusInterface(service, path, interface, bus)
                name = connection.call("Get", "org.bluez.Device1", "Name").arguments()[0]
                address = connection.call("Get", "org.bluez.Device1", "Address").arguments()[0]
                print(f"{address} {name}")

    def _bluezNodes(self):
        service = "org.bluez"
        path = "/org/bluez"
        interface = "org.freedesktop.DBus.Introspectable"
        bus = QtDBus.QDBusConnection.systemBus()
        connection = QtDBus.QDBusInterface(service, path, interface, bus)
        hciNodes = ET.fromstring(connection.call("Introspect").arguments()[0])

        nodes = {}
        for node in hciNodes.iter("node"):
            if node.attrib:
                hci = node.attrib["name"]
                connection = QtDBus.QDBusInterface(service, f"{path}/{hci}", interface, bus)
                devNodes = ET.fromstring(connection.call("Introspect").arguments()[0])
                nodes[hci] = []
                for device in devNodes.iter("node"):
                    if device.attrib:
                        nodes[hci].append(device.attrib["name"])
        return nodes


if __name__== '__main__':
    app = QtWidgets.QApplication([])
    daemon = Main()
    sys.exit(app.exec_())
Output:
hci0 00:1B:XX:DF:XX:A4 PLAYSTATION(R)3 Controller 04:FE:XX:86:XX:16 JBL Flip 4 05:21:XX:8E:XX:25 PLAYSTATION(R)3 Controller 98:D3:XX:40:XX:2C HC05