Modifying bluetoothctl automation script - How to remove output from expect & spawn?

Solution 1:

You don't really need to use expect and can give commands to bluetoothctl directly as arguments.

# ...
bluetoothctl -- power on
bluetoothctl -- connect $mac
# ...

For a more complete example, here is the full Bash script that I use in Ubuntu 20.04 for my BT radio :

#!/bin/bash

mac="90:03:B7:17:00:08" # DEH-4400BT

if [ "$1" = "off" ]; then
    bluetoothctl -- disconnect
    exit $?
fi

# turn on bluetooth in case it's off
rfkill unblock bluetooth

bluetoothctl -- power on
bluetoothctl -- connect $mac

# Use it as default output for PulseAudio

sink=$(pactl list short sinks | grep bluez | awk '{print $2}')

if [ -n "$sink" ]; then
    pacmd set-default-sink "$sink" && echo "OK default sink : $sink"
else
    echo could not find bluetooth sink
    exit 1
fi

This assumes the destination $mac is already paired of course.