-
-
Save RamonGilabert/046727b302b4d9fb0055 to your computer and use it in GitHub Desktop.
#!/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 |
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?
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.
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!
apt-get install expect
my 2 cents
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
Just what i need it. thanks.
Just a quick question is it really need it do the scan off? i skipped that step.
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
#!/usr/bin/expect -f
if {$argc != 2} {
puts "No arguments."
exit 1
}
# First argument needs to be the device MAC address;
# ... second argument needs to be the PIN.
set DEV [lindex $argv 0]
set PIN [lindex $argv 1]
# Do not wait after getting a response.
set timeout -1
# bluetoothctl is in $PATH
spawn bluetoothctl
# Wait for complete startup.
expect "Agent registered"
# If the device is already paired, we remove it first, so we are in a known state of affairs;
# ... we also handle the case in which the device is not already paired.
send "remove $DEV\r"
expect -re "removed|not available"
send "scan on\r"
# Only scan until we see a response from the device;
# ... if the device is not on or in range, this will block the script in scanning;
# ... please make sure the device is: 1) in range, 2) powered on and 3) discoverable.
expect "Device $DEV"
# Stop scanning.
send "scan off\r"
expect "Discovery stopped"
# Request pairing.
send "pair $DEV\r"
expect "Enter PIN code:"
# Send PIN.
send "$PIN\r"
expect "Pairing successful"
This is a general purpose script to programatically pair with a device from a shell script. License GPL2.
But no one mentioned how you discover the mac address between hundred devices around you.
name: is the name of the device you want to find the MAC
hcitool scan >list.txt
grep "name" list.txt>mydevice.txt
cat mydevice.txt | sed 's/name//' > mac.txt
cat mac.txt |tr -d " \t\n\r" >result.txt
the above works but hcitool is depricated. how do you write the same script for bluetoothctl
Thanks in advance
No PIN in my case, it just asks if I accept pairing, have to type yes
. I've come to this:
#!/usr/bin/expect -f
set timeout 5
spawn bluetoothctl
set address [lindex $argv 0]
set pairExecuted 0
# Defines procedure (function) `pair`, `address` is the parameter
proc pair {address} {
send "scan on\n"
sleep 3
send "scan off\n"
send "pair $address\n"
}
send "agent on\n"
send "power on\n"
expect "Changing power on succeeded" {
send "devices\n"
expect "Device ${address}" {
send "remove ${address}\n"
expect "Device has been removed" {
# Calls procedure `pair` passing `address` as argument (defined at the top)
puts [pair ${address}]
# Increments `pairExecuted` variable, so the next
# check (if statement below) will know this has already been executed
incr pairExecuted
}
}
# Checks if the pair procedure has not yet been executed
# this avoids executing it twice
# in other words: if procedure `pair` not yet executed, execute it
if {$pairExecuted == 0}
puts [pair ${address}]
}
}
expect "Accept pairing (yes/no):" { send "yes\n" }
expect "Pairing successful" { send "exit\n" }
expect eof
About the language used here: https://www.tcl-lang.org/
Awesome script thanks so much!
No PIN in my case, it just asks if I accept pairing, have to type
yes
. I've come to this:#!/usr/bin/expect -f set timeout 5 spawn bluetoothctl set address [lindex $argv 0] set pairExecuted 0 # Defines procedure (function) `pair`, `address` is the parameter proc pair {address} { send "scan on\n" sleep 3 send "scan off\n" send "pair $address\n" } send "agent on\n" send "power on\n" expect "Changing power on succeeded" { send "devices\n" expect "Device ${address}" { send "remove ${address}\n" expect "Device has been removed" { # Calls procedure `pair` passing `address` as argument (defined at the top) puts [pair ${address}] # Increments `pairExecuted` variable, so the next # check (if statement below) will know this has already been executed incr pairExecuted } } # Checks if the pair procedure has not yet been executed # this avoids executing it twice # in other words: if procedure `pair` not yet executed, execute it if {$pairExecuted == 0} puts [pair ${address}] } } expect "Accept pairing (yes/no):" { send "yes\n" } expect "Pairing successful" { send "exit\n" } expect eofAbout the language used here: https://www.tcl-lang.org/
i can't seem to use it. i get
": no such file or directory
Thanks!
For new comers:
Install expect:
I use without sudo, and without the
-a
switch which isn't supported anymore, so the final script is:run the script like so: