Last active
October 12, 2023 18:24
Bluetoothctl automation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
the above works but hcitool is depricated. how do you write the same script for bluetoothctl
Thanks in advance