Skip to content

Instantly share code, notes, and snippets.

@RamonGilabert
Last active October 12, 2023 18:24
Show Gist options
  • Save RamonGilabert/046727b302b4d9fb0055 to your computer and use it in GitHub Desktop.
Save RamonGilabert/046727b302b4d9fb0055 to your computer and use it in GitHub Desktop.
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
@goldylucks
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

apt-get install expect

my 2 cents

@jbh
Copy link

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
Copy link

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

@masterchop
Copy link

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

@AleXSR700
Copy link

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

@psihozefir
Copy link

psihozefir commented Oct 26, 2021

#!/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.

@MariwanJ
Copy link

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

@aviladev
Copy link

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/

@srosato
Copy link

srosato commented Jan 3, 2022

Awesome script thanks so much!

@NeonLightning
Copy link

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/

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