Skip to content

Instantly share code, notes, and snippets.

@higstar
Forked from RamonGilabert/bluetooth.sh
Created September 15, 2017 11:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save higstar/f07614088f7b545ce8fbbd98e213493a to your computer and use it in GitHub Desktop.
Save higstar/f07614088f7b545ce8fbbd98e213493a to your computer and use it in GitHub Desktop.
Bluetoothctl automation
#!/usr/bin/expect -f
set prompt "#"
set address [lindex $argv 0]
spawn sudo bluetoothctl -a
expect -re $prompt
send "default-agent\r"
expect "Default agent request successful"
send "remove $address\r"
expect "Device"
set timeout 120
send "scan on\r"
expect "Discovery started"
send_user "\nWaiting for $address to appear...\r"
expect "$address"
send_user "\nDone waiting\r"
send "scan off\r"
expect -re $prompt
send "pair $address\r"
expect {
"PIN code:" {
send "1234\r"
send_user "\nShould be paired now.\r"
expect "Paired: yes"
}
"Failed to pair:" {
send_user "\nFailed to pair :(\r"
}
}
send "quit\r"
expect eof
@higstar
Copy link
Author

higstar commented Sep 15, 2017

Removed my pet hate...hard coded sleeps.
Seems to be relatively stable.
I would like to add in an automatic binding (sudo rfcomm bind $available_port $address) but need to figure out how to get $available_port.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment