Search code examples
c++linuxbluetoothdbusbluez

BlueZ5: Event when inbound pairing is complete


I'm writing a C++-application which manages Bluetooth connections for an embedded device. I talk to BlueZ5 via D-Bus under Linux.

As first steps in implementing inbound pairing I did the following:

  • Register "NoInputNoOutput" agent via the AgentManager1 interface
  • set pairable on and pairable timeout as well as discoverable on and discoverable timeout via the Adapter1 interface

Now I need an event that tells me that a new device was paired, so I can trust it and accept SPP connections. But I couldn't find such an event in the spec yet (https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc).

Is there such an event? It seems it as bluetoothctl emits a message like

[NEW] Device 44:55:66:11:22:33 Foo Bar

as soon as device is paired...

Can somebody tell me which event I have to listen for?

Or do I have to poll? I don't think bluetoothctl polls as it reacts really quickly.


Solution

  • From here

    boolean Connected [readonly]

    Indicates if the remote device is currently connected. A PropertiesChanged signal indicate changes to this status.

    When the new device is added, InterfaceAdded signal is broadcasted on interface=org.freedesktop.DBus.ObjectManager. See the below signal captured using dbus-monitor. Check for the property Connected.

    signal time=1558128293.155096 sender=:1.2 -> destination=(null destination) serial=65 path=/; interface=org.freedesktop.DBus.ObjectManager; member=InterfacesAdded
       object path "/org/bluez/hci0/dev_F0_D7_AA_AA_0C_41"
       array [
          dict entry(
             string "org.freedesktop.DBus.Introspectable"
             array [
             ]
          )
          dict entry(
             string "org.bluez.Device1"
             array [
                dict entry(
                   string "Address"
                   variant                   string "F0:D7:AA:AA:0C:41"
                )
                dict entry(
                   string "Name"
                   variant                   string "Moto"
                )
                dict entry(
                   string "Alias"
                   variant                   string "Moto"
                )
                dict entry(
                   string "Class"
                   variant                   uint32 5898764
                )
                dict entry(
                   string "Icon"
                   variant                   string "phone"
                )
                dict entry(
                   string "Paired"
                   variant                   boolean false
                )
                dict entry(
                   string "Trusted"
                   variant                   boolean false
                )
                dict entry(
                   string "Blocked"
                   variant                   boolean false
                )
                dict entry(
                   string "LegacyPairing"
                   variant                   boolean false
                )
                dict entry(
                   string "Connected"
                   variant                   boolean true
                )
                dict entry(
                   string "UUIDs"
                   variant                   array [
                      ]
                )
                dict entry(
                   string "Adapter"
                   variant                   object path "/org/bluez/hci0"
                )
             ]
          )
          dict entry(
             string "org.freedesktop.DBus.Properties"
             array [
             ]
          )
       ]
    

    If device is already added then you get PropertiesChanged signal on interface=org.freedesktop.DBus.Properties. See below capture, its a log on disconnect, but the one above could help you to receive signal when device is connected.

    signal time=1558128303.204016 sender=:1.2 -> destination=(null destination) serial=71 path=/org/bluez/hci0/dev_F0_D7_AA_AA_0C_41; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
       string "org.bluez.Device1"
       array [
          dict entry(
             string "Connected"
             variant             boolean false
          )
       ]
       array [
       ]