Can a object name, registered by busctl, be different from the address property, when it comes to bluetooth?

484 views Asked by At

Background for the question

So im developing a small posix shell script that is meant to revolve around Bluetooth and since im considering "publishing" it to the public im at the phase of both optimizing it and later share the code with others so i can get it reviewed.


The question

The way im getting every bluetooth device that paired with the host, ie the device running this shell script, is:

busctl tree org.bluez > $tempfile
while read mac; do
    t=$(echo "${mac##*/dev_}"| grep -v "└─/org")
    a=${t%%/*}
    #Discard repeats due to there being "sub-adresses"
    if [ -z "$a" ] || [ "$a" != "$t" ]; then
        continue;
    fi
    #There's other code running here

done < $tempfile

and then having the object names inside the loop i just use them to return their address, and also other properties which i won't go into detail, like so

c=$(busctl get-property $SERVICE $OBJ_TEMPLATE"$a" $INTERFACE Address)
c=${c##s }
# will return something like " 00:00:00:dd:33:22"

which for the most part is useless as from what I've seen the object name given by busctl tree, when formatted, corresponds to what it will reply from the property address

The thing is im scared that due to some kind of corruption on the busctl part or on the device part those could mismatch, where both could still function but only if i treat the object name as an alias and not as a fixed generic fixed naming convention

exe: when doing busctl tree it will reply on one of the lines: ├─/org/bluez/hci0/dev_00_00_00_00_B5_C8 but get-property will return s 22:33:21:25:AA:B4

1

There are 1 answers

5
ukBaz On

You might be better getting the information on what devices are available from calling GetManagedObjects on the org.freedesktop.DBus.ObjectManager interface.

For example:

busctl call org.bluez / org.freedesktop.DBus.ObjectManager GetManagedObjects

To get just the devices that are in the cache (and their address) you could easily grep that information:

 busctl call org.bluez / org.freedesktop.DBus.ObjectManager GetManagedObjects | grep -Po '"org.bluez.Device1".*?"Address" s \S+'

To find if devices are paired you can add that to the grep:

busctl call org.bluez / org.freedesktop.DBus.ObjectManager GetManagedObjects | grep -Po '"org.bluez.Device1".*?"Address" s \S+\s+.*?"Paired" b \S+'