6

I have two Bluetooth trackpads, one at work and one at home. Is there a way to select one or the other from the command line. My current workflow is going up to the bluetooth menu, hovering over to home or office and clicking on Connect, but I wish I could do this from the terminal.

  • Can you clarify your answer so that we know what you've already tried re the command line? Otherwise you may get a lot of wasted suggestions/answers. Also, if it's relevant, you may want to expand on why you want to connect via the terminal - I ask because if it's to get around see sort of problem then there may be other better options to get around it rather than using the terminal. – Monomeeth May 2 '16 at 3:03
4

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

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
2

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

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
2
+50
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
  • Thanks! This is exactly what I needed. Much faster than doing it with applescript and there's no need to enter a sudo password. Instead of killall I just hit ctrl+c again – nachocab May 4 '16 at 20:54
  • I think I spoke too soon. Your command makes it possible to connect and disconnect the last enabled bluetooth command, but changing from one to the other fails with this error: Create connection failed (0x4) for device: 7C-C3-A1-4D-CF-99 – nachocab May 5 '16 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .