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.
-
Here's an answer using Applescipt than calling it from the command line–is that good enough?– JMY1000Commented May 1, 2016 at 23:45
-
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 ♦Commented May 2, 2016 at 3:03
5 Answers
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
-
I used the code above. One note though, if your device name has an apostrophe (for example, "Bob’s Airpods", then you may need to use a non-standard character. It seems that devices that were configured on a phone are likely to use the U+2019 character, while our IDE's usually use U+0027. I copy/pasted the fancy apostrophe and then this worked fine. I'm using Alfred to trigger it and it only takes fractions of a second– TimSumCommented Aug 18, 2020 at 4:31
I've just done it with https://github.com/toy/blueutil.
Installed it from MacPorts:
$ sudo port install blueutil
found out the ID of my device:
$ blueutil --paired
address: 03-5e-1c-4a-a8-11, not connected, not favourite, not paired, name: "Los Headphones", recent access date: 2020-03-17 17:58:09 +0000
...
Connected:
$ blueutil --connect 03-5e-1c-4a-a8-11
-
1
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....
- Kill the daemon
- Waits for 3 seconds to ensure it has been killed (you can change this if you like)
- 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
- You will have to enter your password every time. Launchctl is a system command and requires elevated privileges.
- 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....
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– nachocabCommented May 4, 2016 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
– nachocabCommented May 5, 2016 at 13:14