Search code examples
bluetooth-lowenergybluez

Bluez BLE Connections Monitoring using DBUS-Python


I was able to advertise the BLE and setup GATT Services and Characteristics by following the "example_advertisement" and "example-gatt-server" in Bluez Examples. How can I know from DBUS when a BLE client is connected and when it is disconnected, using similar DBUS binding for Python? Which DBUS API do I look into?


Solution

  • There is another example that you can look at: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/test-discovery

    When BlueZ/DBus learns of a new remote device, then the InterfacesAdded signal gets sent. When a remote device goes from disconnected, to connected. Then that is a property change on the device and the PropertiesChanged signal gets sent. This is why there is the code in the above example they use add_signal_receiver to add callbacks for both signals.

        bus.add_signal_receiver(interfaces_added,
                dbus_interface = "org.freedesktop.DBus.ObjectManager",
                signal_name = "InterfacesAdded")
    
        bus.add_signal_receiver(properties_changed,
                dbus_interface = "org.freedesktop.DBus.Properties",
                signal_name = "PropertiesChanged",
                arg0 = "org.bluez.Device1",
                path_keyword = "path")
    

    As a side note, the DBus bindings used in the Buez examples are not the only ones available: https://www.freedesktop.org/wiki/Software/DBusBindings/