20

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?

4 Answers 4

7

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.

1
  • 1
    blueutil --recent i run this option have a list but blueutil --inquiry as no new devices are avaiable. Apr 10, 2021 at 1:56
4

The tool blueutil uses private APIs from the IOBluetooth framework to interact with bluetooth devices. You are able to list recent devices using:

blueutil --recent

and connect to a specific device using:

blueutil --connect $device_adress
2

This seems a little bit "hacky", but you can do this by using an oascript which emulates the GUI-interactions needed for connecting: https://stackoverflow.com/a/23018886/5269642

I couldn't really find any documented API's for bluetooth unfortunately, the oascript-approach was the best I could find/think of so far.

0

I'm using this:

https://github.com/lapfelix/BluetoothConnector

TL;DR

$ brew install bluetoothconnector

$ BluetoothConnector # gets the address of your connected devices

$ BluetoothConnector 00-00-00-00-00-00 --notify # toggles the connection

Replace the 00-00-00-00-00-00 with your MAC address

You must log in to answer this question.

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