MacOS – Pair with a Bluetooth device over Terminal

bluetoothmacosterminal

I am trying to connect and pair a Bluetooth device from the command line. I can do it over the GUI using the standard process but I am looking to automate the whole process.

I am looking for commands that can be executed in a shell script to connect to the Bluetooth device. Just like Linux has "hcitool", "pand" commands, I am trying to find equivalent commands for macOS.

How can I accomplish this?

Best Answer

blueutil (available via HomeBrew) is the answer. I just used it following the guidelines in this blog post.

In my case, I had an iMac with macOS Catalina that had no trackpad paired. I was stuck at the "Send Apple Crash Analytics?" screen with no keyboard controls. Using SSH, I connected to the machine and used the following commands to pair and then connect the trackpad without access to the GUI. (First, I had to turn the trackpad into Discoverable mode. The default PIN for Apple pointer devices is 0000.)

% blueutil --inquiry
address: d0-a6-xx-xx-xx-xx, not connected, not favourite, not paired, name: "-", recent access date: -
% blueutil --pair d0-a6-xx-xx-xx-xx
Type pin code (up to 16 characters) for "Cheshire, Josh’s Trackpad" (d0-a6-xx-xx-xx-xx) and press Enter: 0000
% blueutil --connect d0-a6-xx-xx-xx-xx

At that point, the trackpad worked.