Moode Forum
Auto-trust bluetooth devices - Printable Version

+- Moode Forum (https://moodeaudio.org/forum)
+-- Forum: moOde audio player (https://moodeaudio.org/forum/forumdisplay.php?fid=3)
+--- Forum: Support (https://moodeaudio.org/forum/forumdisplay.php?fid=7)
+--- Thread: Auto-trust bluetooth devices (/showthread.php?tid=5648)



Auto-trust bluetooth devices - Tim Curtis - 06-22-2023

Note: This was originally posted to the moode Git repo issue tracker by user @aero31aero but I though I'd move it here in case anyone has interest. Refer to https://github.com/moode-player/moode/issues/614

============

Hi, I'm not sure if you're already aware of this, but apparently Linux's bluetooth stack itself doesn't play well with 'untrusted' devices. 

This has been my experience back with my old laptop as well as now with Moode.

Without trusting the devices, my phones aren't able to reliably connect to Moode's bluetooth despite having been successfully paired. I didn't dig deep into the logs so I don't know what the exact errors are, but on the phone end, there are no error messages; they just fail to connect.

I've been using a particular fix for ~2 years now which I've now had to redeploy on my new installation too, so I thought I'd share:

Code:
$ tail -n +1 auto-trust*
==> auto-trust.service <==
[Unit]
Description=Auto trusts all paired bluetooth devices.
Wants=auto-trust.timer

[Service]
Type=oneshot
ExecStart=/home/pi/auto-trust.sh

[Install]
WantedBy=multi-user.target

==> auto-trust.sh <==
#! /usr/bin/env bash
for mac in $(sudo bluetoothctl devices | awk '/^Device/ {print $2}'); do sudo bluetoothctl trust $mac;done

==> auto-trust.timer <==
[Unit]
Description=Auto trust bluetooth devices.
Requires=auto-trust.service

[Timer]
Unit=auto-trust.service
OnUnitInactiveSec=10s
OnBootSec=10s

[Install]
WantedBy=timers.target

My solution is definitely a hack, but I figure you folks would be able to implement something cleaner with this info. Much thanks!



RE: Auto-trust bluetooth devices - TheOldPresbyope - 06-22-2023

I thought Bluetooth trust had to do with something other than connecting. I’ve only seen the topic discussed in the context of unlocking a mobile device just by having a trusted device nearby.

Can’t remember if bluetoothctl was showing my connected devices were trusted or untrusted.


Regards,
Kent


RE: Auto-trust bluetooth devices - Tim Curtis - 06-22-2023

IIRC Bluetooth comms setup sequence is as follows:

Trust
Pair
Connect

If the device is not trusted before pairing a passcode will be required. This would be indicated by a passcode prompt on the client trying to pair with the untrusted device.

In moOde /var/www/util/blu-control.sh script all scanned devices are automatically trusted for 90 secs.

Code:
# Set to the value of the TemporaryTimeout param in /etc/bluetooth/main.conf
TRUSTED_DEVICE_TIMEOUT=90

.
.
.

# Trust scanned devices
TRUST() {
    echo "** Trust expires in $TRUSTED_DEVICE_TIMEOUT seconds"
    unset btdev
    mapfile -t btdev < <(echo -e "devices\nquit"  | bluetoothctl | grep "^Device" |  while IFS= read -r line ; do echo "$line" |cut -d " " -f2- ; done )
    for i in "${btdev[@]}" ; do
       echo "** $i"
       y="$( echo $i | cut -d " " -f1)"
       echo -e "trust $y\nquit"  | bluetoothctl >/dev/null
       #sleep 1
    done
    echo "**"
}



RE: Auto-trust bluetooth devices - TheOldPresbyope - 06-22-2023

Doh! My memory is getting worse by the day.

Regards,
Kent