Apple - How to select the Bluetooth device using command line?

This open-source CLI app BluetoothConnector seems like a good solution. I tested and confirmed it's working as recently as macOS 10.15.2.

It's available on GitHub as well as Homebrew:

$ brew install bluetoothconnector
$ BluetoothConnector --connect 00-11-22-33-44-55 --notify
$ BluetoothConnector --disconnect 00-11-22-33-44-55

I ended up going with this modified version from this answer and creating two files, one with "Home Trackpad" and another with "Office Trackpad". It works, but it takes a few seconds to complete

tell application "System Events" to tell process "SystemUIServer"
  set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
  click bt
  tell (first menu item whose title is "Home Trackpad") of menu of bt
    click
    tell menu 1
      if exists menu item "Connect"
        click menu item "Connect"
        return "Connecting..."
      else
        click bt  -- close main dropdown to clean up after ourselves
        return "No connect button; is it already connected?"
      end if
    end tell
  end tell
end tell

AFIK, there is no built in command line utility to manage individual Bluetooth connections.

In fact, the man page man blued(OS X Bluetooth daemon) specifically states:

The Bluetooth daemon handles SDP transactions, link key management, and incoming connection acceptance. It cannot be used directly by the user. (Emphasis mine)

So, it seems that you are limited to loading/unloading the BT daemon from the command line. That may be helpful, however. If your device is active, OS X will connect to the device automatically upon start up. In other words, it will automatically connect to any device in range when the daemon starts.

Get the Daemon Status If we issue the command defaults read /Library/Preferences/com.apple.Bluetooth.plist ControllerPowerState

It responds back with a 1 or 0 depending whether BT daemon is running or not; 1 being up, 0 being down.


Unload the Daemon

Issuing the command sudo killall blued should kill the BT daemon. The downside here is that if you have a BT keyboard, it will stop working.


(Re)Starting the Daemon

If we issue the command: launchctl start com.apple.blued it will tell the daemon to (re)start. After doing so, it should detect the device within range.

Putting it all Together Putting it together as one line, we can issue the command

sudo killall blued && sleep 3 && launchctl start com.apple.blued

What this does is....

  1. Kill the daemon
  2. Waits for 3 seconds to ensure it has been killed (you can change this if you like)
  3. Starts the daemon.

Now to Make it Easier

To make this easier we give it an alias. You can call the alias whatever you like; I just used "btrestart" because it makes sense in this context.

alias btrestart=sudo killall blued && sleep 3 && launchctl start com.apple.blued

Now, anywhere in your terminal shell, if you type btrestart it will issue the command. Once you have it working, you can add it to your .bash_profile so that the alias is persistent across reboots:

echo alias btrestart=sudo killall blued && sleep 3 && launchctl start com.apple.blued >> ~/.bash_profile


A Couple Caveats

  1. You will have to enter your password every time. Launchctl is a system command and requires elevated privileges.
  2. All of your BT devices will get disconnected and reconnected. If you only have one or two BT devices, this may not be much of an issue

I hope this gets you going in the right direction....

Tags:

Bluetooth