Skip to content

Instantly share code, notes, and snippets.

@0x4C4A
Last active April 16, 2017 12:48
Show Gist options
  • Save 0x4C4A/564b37a8a5b630e41802799d257dc972 to your computer and use it in GitHub Desktop.
Save 0x4C4A/564b37a8a5b630e41802799d257dc972 to your computer and use it in GitHub Desktop.
A script to re-pair with a bluetooth audio receiver, when it's been paired to another device. I use this, to pair/auto-connect on boot.
#!/usr/bin/expect -f
# Runs bluetoothctl and attempts to connect with bluetooth device
# If connection fails, tries to unpair, pair and then connect again
# My specific device apparently can't be paired to multiple hosts, so it needs this
# Change this to whatever your device's bluetooth ID is
set ID "BC:FD:5B:00:14:28"
spawn "bluetoothctl"
expect "# "
send "power on\n"
expect "# "
send "trust $ID\n"
expect "# "
send "connect $ID\n"
expect {
"Failed to connect: org.bluez.Error.Failed" {
send "remove $ID\n"
expect "[NEW] Device $ID"
send "pair $ID\n"
expect "Pairing succesful"
send "connect $ID\n"
}
}
expect "Connection successful"
expect "# "
send "quit\n"
expect eof
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment