This script worked for me to automate pairing an Amazon Echo with a Pi 4 running Raspbian Buster.
Is this the most elegant way to automate connecting an Amazon Echo speaker? I would suspect not. bluetoothctl
is not very script-friendly, so this is best-efforts basis folks. However, I think it's a great starting point to fix a problem I can see at least 3,000 other geeks are interested in solving at the time of me writing this.
Basically, the way I attacked the problem was to filter the MAC address of the Echo speaker and then operate on it as a variable. Again, this script could probably do with a big tidy-up, so if anybody has ideas on improving it, they're welcome folks.
#!/bin/bash
# Add some audio and Bluetooth packages
apt-get install blueman pulseaudio pavucontrol pulseaudio-module-bluetooth
echo
echo
read -p 'Say "ALEXA PAIR" and then press ENTER to continue'
echo
echo
# Capture Mac Address of Alexa Speaker by grepping for the string "Echo" from the bluetoothctl scan:
ALEXAMACADDRESS=$(sudo timeout 10s bluetoothctl scan on >> /home/pi/scan-output.txt | cat /home/pi/scan-output.txt | grep -m1 '.*Echo.*' | awk '{print $3}')
bluetoothctl pair $ALEXAMACADDRESS
bluetoothctl connect $ALEXAMACADDRESS
bluetoothctl trust $ALEXAMACADDRESS
Please note that once paired, it's down to you to set the Echo as the audio output where required- T