If you’re like me, you try to automate everything, even the small stuff. I have a few Bluetooth devices that I occasionally use with my MacBook Pro:
- Apple Magic Trackpad 2
- Apple Magic Keyboard
- Bose QuietComfort 35
- Sennheiser PXC550
Getting Bluetooth devices powered on and connected isn’t always the easiest process, especially if you have pairings with multiple devices. Sometimes, Bluetooth devices don’t automatically reconnect immediately, even if they’re already paired up. To streamline this process, I’ll switch off Bluetooth on my MacBook Pro, wait a few seconds, and then switch it back on. This power cycling process is typically done through the GUI, but I wanted to find a way to automate it.
brew install blueutil
Once installed, you should be able to call
blueutil from your shell. My personal preference is PowerShell, but you might stick with the default, Bash.
To list the Bluetooth devices that are already paired with your MacBook Pro, use the following command:
On my system, I received some output similar to the following:
address: 1c-36-bb-08-4d-77, connected (slave, -89 dBm), not favourite, paired, name: "Sullivan, Trevor’s Trackpad", recent access date: 2019-07-30 15:31:33 +0000 address: 04-52-c7-31-73-9f, not connected, not favourite, paired, name: "Vector", recent access date: 2019-07-28 21:49:55 +0000 address: 70-f1-93-1b-d4-3f, connected (slave, -84 dBm), not favourite, paired, name: "Sullivan, Trevor’s Keyboard", recent access date: 2019-07-30 21:30:39 +0000 address: 00-26-81-35-de-27, not connected, not favourite, paired, name: "PXC 550", recent access date: 2019-07-30 19:32:49 +0000
In order to connect to one of these Bluetooth devices, you can use the
--connect argument, and pass in the physical address of the device that you’d like to connect to.
blueutil --connect 00-26-81-35-de-27
If you’d rather connect the Bluetooth device to a different computer, that has already connected to your MacBook Pro, you can forcibly disconnect a device using the
--disconnect argument, and pass in the device’s physical address.
blueutil --disconnect 00-26-81-35-de-27
If you’d like to put your MacBook Pro into “discoverable” mode, so that you can pair to it from another device, use the following command:
blueutil --discoverable 1
If you haven’t paired your Bluetooth device to your MacBook Pro yet, you can search for discoverable devices and then initiate pairing, using the following commands:
blueutil --inquiry # Initiates discovery of unpaired devices that are in discovery mode blueutil --pair 00-26-81-35-de-27 0000 # Pair with the specified device and PIN
Power Cycling Bluetooth on MacBook Pro
If you’d like to power cycle the Bluetooth radio on your MacBook Pro, use the commands below.
blueutil --power 0 sleep 2 blueutil --power 1
As you can see, the blueutil program provides an easy-to-consume interface to managing the Bluetooth radio and your Bluetooth devices from your MacBook pro. All of the core Bluetooth capabilities are exposed through this command, using clearly documented, plain-English arguments.