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 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 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 higstar commented Sep 15, 2017

Great help, thanks @RamonGilabert.

@Divyaera

This comment has been minimized.

Copy link

@Divyaera 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 chemage commented Jul 30, 2018

Awesome script!

@kakposoe

This comment has been minimized.

Copy link

@kakposoe kakposoe commented Mar 11, 2019

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

@JeffRossMT

This comment has been minimized.

Copy link

@JeffRossMT 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 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
@ksonbol

This comment has been minimized.

Copy link

@ksonbol ksonbol commented Nov 14, 2019

Didn't work for Ubuntu 18.04. I also had to remove and re-pair the device. Any idea how to make it work for 18.04?

@jerlam06

This comment has been minimized.

Copy link

@jerlam06 jerlam06 commented Apr 6, 2020

Didn't work for Ubuntu 18.04. I also had to remove and re-pair the device. Any idea how to make it work for 18.04?

Here is the script I adapted to my need:

#!/usr/bin/expect -f

set prompt "#"
set address "MY_MAC_ADDRESS"

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"
send "connect $address\r"
sleep 5
send "quit\r"
expect eof

This basically removes the device from the paired list, then connects it again.

@stratus-ss

This comment has been minimized.

Copy link

@stratus-ss stratus-ss commented Apr 13, 2020

Didn't work for Ubuntu 18.04. I also had to remove and re-pair the device. Any idea how to make it work for 18.04?

Here is the script I adapted to my need:

#!/usr/bin/expect -f

set prompt "#"
set address "MY_MAC_ADDRESS"

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"
send "connect $address\r"
sleep 5
send "quit\r"
expect eof

This basically removes the device from the paired list, then connects it again.

Thanks for this.. this was driving me crazy!

@gopakumar

This comment has been minimized.

Copy link

@gopakumar gopakumar commented Apr 26, 2020

apt-get install expect

my 2 cents

@jbh

This comment has been minimized.

Copy link

@jbh jbh commented Jul 2, 2020

This is great, thanks. I'm finding a lot of bugs as I go along, though. Sometimes it doesn't wait long enough for the device to be discovered. I've increased the wait time from scan to 15 seconds, and it still sometimes will reply "Device unavailable" after waiting 15 seconds. Also, sometimes the script completely crashes my bluetooth service, and I have to restart bluetooth. This is what I have so far:

#!/usr/bin/expect -f
# Bluetooth seems to get messed up after dual booting
# Winblows for gaming... This script resets my bluetooth

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

spawn bluetoothctl
expect -re $prompt
send "scan on\r"
send_user "\nWaiting for device.\r"
# I thought I could rely on this expect to make sure the device is discovered and ready
# It finds it, but this still isn't good enough... I still need a sleep
expect -re "\\\[NEW\\\] Device $address"
send_user "\nFound deivce.\r"
sleep 5
send "remove $address\r"
sleep 2
expect -re $prompt
send "pair $address\r"
sleep 2
send "connect $address\r"
sleep 3
send "trust $address\r"
sleep 2
send_user "\n$address should be paired now.\r"
send "scan off\r"
send "quit\r"
expect eof

Anyone found a way to use expect or something similar to wait for success of each command? I'm new to expect since discovering this script.

@santhosh12992

This comment has been minimized.

Copy link

@santhosh12992 santhosh12992 commented Aug 31, 2020

if you are looking for something programmatic using python => https://gist.github.com/castis/0b7a162995d0b465ba9c84728e60ec01

@masterchop

This comment has been minimized.

Copy link

@masterchop masterchop commented Sep 12, 2020

Just what i need it. thanks.
Just a quick question is it really need it do the scan off? i skipped that step.

@AleXSR700

This comment has been minimized.

Copy link

@AleXSR700 AleXSR700 commented Jan 6, 2021

Hi everyone,
this script was exactly what I was looking for :)
It works great with my BT mouse, however iit does not work with my BT keyboard as the keyboard requires me to enter a code. The script does not show me the code so it does not pair properly,

Could someone help me change the script to work with a BT keyboard requiring a pairing code to be entered?

Thank you
Alex

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