How to select the Bluetooth device using command line?

Solution 1:

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

Solution 2:

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

Solution 3:

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....

Solution 4:

blued listall

This Terminal command will tell you all currently 'known' devices. You're going to have to figure out (by reading between the lines) which one is which (assuming you've named your trackpads something "nice", like "home" or "work", etc.)

I did this, and got:

2016-05-03 20:14:58.392 blued[3852:507] hostControllerOnline - Number of Paired devices = 3, List of Paired devices = (
    "d8-96-95-e0-3e-f8",
    "c4-2c-03-b7-87-57",
    "90-84-0d-e4-7b-41"
)
link key found for device: c4-2c-03-b7-87-57
2016-05-03 20:15:04.637 blued[3852:507] link key found for device: c4-2c-03-b7-87-57
2016-05-03 20:15:04.639 blued[3852:507] [setSystemPreference] syncs returns false
2016-05-03 20:15:04.639 blued[3852:507] Save link key for device: c4-2c-03-b7-87-57
link key found for device: 90-84-0d-e4-7b-41
2016-05-03 20:15:04.640 blued[3852:507] link key found for device: 90-84-0d-e4-7b-41
2016-05-03 20:15:04.640 blued[3852:507] Save link key for device: 90-84-0d-e4-7b-41
link key found for device: d8-96-95-e0-3e-f8
2016-05-03 20:15:04.640 blued[3852:507] link key found for device: d8-96-95-e0-3e-f8
2016-05-03 20:15:04.641 blued[3852:507] Save link key for device: d8-96-95-e0-3e-f8 
Create connection failed (0x4) for device: D8-96-95-E0-3E-F8

(then i pressed "ctrl-c" to cancel)

Let's assume that I knew that "c4-2c-03-b7-87-57" is 'home'....

next:

blued join c4-2c-03-b7-87-57

This works on my (10.9.5) system. YMMV.

You may also have to add a kill function, as it appears that blued doesn't 'talk' well with Terminal.

So perhaps a possible shell command would be:

blued join c4-2c-03-b7-87-57
killall terminal