|set prompt "#"|
|set address [lindex $argv 0]|
|spawn sudo bluetoothctl -a|
|expect -re $prompt|
|send "remove $address\r"|
|expect -re $prompt|
|send "scan on\r"|
|send_user "\nDone sleeping\r"|
|send "scan off\r"|
|send "trust $address\r"|
|send "pair $address\r"|
|send_user "\nShould be paired now.\r"|
spawn: invalid option -- 'a'
These are what I got, how to use it. Can anyone help?
For new comers:
$ sudo apt install expect
I use without sudo, and without the
run the script like so:
Here is the script I adapted to my need:
This basically removes the device from the paired list, then connects it again.
Thanks for this.. this was driving me crazy!
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.
if you are looking for something programmatic using python => https://gist.github.com/castis/0b7a162995d0b465ba9c84728e60ec01
Could someone help me change the script to work with a BT keyboard requiring a pairing code to be entered?