0

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

Imeguras
  • 436
  • 6
  • 18

1 Answers1

2

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+'
ukBaz
  • 6,985
  • 2
  • 8
  • 31
  • mmm not quite what i want, that call return's every "object" i want every object that represents a previous paired bluetooth device – Imeguras Nov 15 '21 at 15:06
  • The device info in the tree does not show only *paired* devices. It shows devices that BlueZ has currently in its cache. BlueZ has some rules about when and what devices are removed automatically – ukBaz Nov 15 '21 at 15:53
  • alright althought this help's and fixes the problem it does not adress it for SO reason's can cached device's address differ from what busctl tree display's? also thank's i was wondering if there wasn't a better way – Imeguras Nov 16 '21 at 09:14
  • 1
    The working assumption is that an Address of `xx:xx:xx:xx:xx:xx` becomes `dev_xx_xx_xx_xx_xx_xx ` but I don't know if there are any corner cases. For example, resolvable random addresses. The best way is to use `GetManagedObjects` for `org.bluez.Device1`. That output also includes the D-Bus object path if you need it. – ukBaz Nov 16 '21 at 09:27
  • Yes that comment improves the answer but can public address's ever change? even if its due to corruption or people manually overriding the value. Is it just so hard-coded from factory that changing the address would cause the device to stop working? either way when it comes to the code i will take the advice you gave me – Imeguras Nov 16 '21 at 10:17