#!/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 |
This comment has been minimized.
This comment has been minimized.
thank you very much, this code was what i was looking for |
This comment has been minimized.
This comment has been minimized.
Great help, thanks @RamonGilabert. |
This comment has been minimized.
This comment has been minimized.
spawn: invalid option -- 'a' These are what I got, how to use it. Can anyone help? |
This comment has been minimized.
This comment has been minimized.
Awesome script! |
This comment has been minimized.
This comment has been minimized.
Thank you @RamonGilabert. I did not know about expect scriping! |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
Thanks! For new comers: Install expect: $ sudo apt install expect I use without sudo, and without the
run the script like so:
|
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
Here is the script I adapted to my need:
This basically removes the device from the paired list, then connects it again. |
This comment has been minimized.
This comment has been minimized.
Thanks for this.. this was driving me crazy! |
This comment has been minimized.
This comment has been minimized.
apt-get install expect my 2 cents |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
if you are looking for something programmatic using python => https://gist.github.com/castis/0b7a162995d0b465ba9c84728e60ec01 |
This comment has been minimized.
This comment has been minimized.
Just what i need it. thanks. |
This comment has been minimized.
This comment has been minimized.
Hi everyone, Could someone help me change the script to work with a BT keyboard requiring a pairing code to be entered? Thank you |
This comment has been minimized.
Thank you for this! This Expect script was really useful for me. :)