Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bluetoothctl automation
#!/usr/bin/expect -f
set prompt "#"
set address [lindex $argv 0]
spawn sudo bluetoothctl -a
expect -re $prompt
send "remove $address\r"
sleep 1
expect -re $prompt
send "scan on\r"
send_user "\nSleeping\r"
sleep 5
send_user "\nDone sleeping\r"
send "scan off\r"
expect "Controller"
send "trust $address\r"
sleep 2
send "pair $address\r"
sleep 2
send "0000\r"
sleep 3
send_user "\nShould be paired now.\r"
send "quit\r"
expect eof
@devrique

This comment has been minimized.

Copy link

devrique commented Jan 27, 2017

Thank you for this! This Expect script was really useful for me. :)

@Sergey82

This comment has been minimized.

Copy link

Sergey82 commented Jun 3, 2017

thank you very much, this code was what i was looking for

@higstar

This comment has been minimized.

Copy link

higstar commented Sep 15, 2017

Great help, thanks @RamonGilabert.

@Divyaera

This comment has been minimized.

Copy link

Divyaera commented Jul 25, 2018

spawn: invalid option -- 'a'
*** Usage: spawn [-h] [-v] [-jN]
bluez.sh: line 7: expect: command not found
bluez.sh: line 8: send: command not found
bluez.sh: line 10: expect: command not found
bluez.sh: line 11: send: command not found
bluez.sh: line 12: send_user: command not found
bluez.sh: line 14: send_user: command not found
bluez.sh: line 15: send: command not found
bluez.sh: line 16: expect: command not found
bluez.sh: line 17: send: command not found
bluez.sh: line 19: send: command not found
bluez.sh: line 21: send: command not found
bluez.sh: line 23: send_user: command not found
bluez.sh: line 24: send: command not found
bluez.sh: line 25: expect: command not found

These are what I got, how to use it. Can anyone help?

@chemage

This comment has been minimized.

Copy link

chemage commented Jul 30, 2018

Awesome script!

@kakposoe

This comment has been minimized.

Copy link

kakposoe commented Mar 11, 2019

Thank you @RamonGilabert. I did not know about expect scriping!

@JeffRossMT

This comment has been minimized.

Copy link

JeffRossMT commented Jun 8, 2019

With bluetoothctl 5.48 the command line switch -a is no longer valid and must be removed.

I haven't used expect in a very long time--it was good to find this! Thank you.

@goldylucks

This comment has been minimized.

Copy link

goldylucks commented Jul 26, 2019

Thanks!

For new comers:

Install expect:

$ sudo apt install expect

I use without sudo, and without the -a switch which isn't supported anymore, so the final script is:

#!/usr/bin/expect -f

set prompt "#"
set address [lindex $argv 0]

spawn bluetoothctl
expect -re $prompt
send "remove $address\r"
sleep 1
expect -re $prompt
send "scan on\r"
send_user "\nSleeping\r"
sleep 5
send_user "\nDone sleeping\r"
send "scan off\r"
expect "Controller"
send "trust $address\r"
sleep 2
send "pair $address\r"
sleep 2
send "0000\r"
sleep 3
send_user "\nShould be paired now.\r"
send "quit\r"
expect eof

run the script like so:

$ /path/to/script.sh 00:00:00:00:00  # replace 00:00:00:00:00 with the device mac address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.