Simple macOS CLI to connect/disconnect a Bluetooth device. Useful for AirPods or other Bluetooth headphones.
Simple macOS CLI to connect/disconnect a Bluetooth device. I made it to easily connect my BeatsX earbuds (I thought the W1 chip would make the switch from my iPhone to my Mac seamless, but we’re not there yet, apparently). There’s probably a program that already does this but I didn’t find it.
You can download a compiled version here: https://github.com/lapfelix/BluetoothConnector/releases
If you have issues running the compiled binary (permission denied
error), try running this command in Terminal:
chmod +x /path/to/BluetoothConnector
brew install bluetoothconnector
mint install lapfelix/BluetoothConnector
swift package update
swift build -c release
mv .build/release/BluetoothConnector /usr/local/bin/BluetoothConnector
Replace 00-00-00-00-00-00
with your device’s MAC address. You can get it by alt-clicking the Bluetooth menu icon or by running BluetoothConnector without any arguments
BluetoothConnector 00-00-00-00-00-00 --notify
BluetoothConnector --connect 00-00-00-00-00-00 --notify
BluetoothConnector -c 00-00-00-00-00-00 --notify
BluetoothConnector --disconnect 00-00-00-00-00-00
BluetoothConnector -d 00-00-00-00-00-00
You can have BluetoothConnector send a notification to say what it did (connect or disconnect), if the action succeeded or get an error description if something goes wrong
BluetoothConnector 00-00-00-00-00-00 --notify
To reduce Notification Center pollution, I’d recommend turning off “Show in Notification Center” for the Script Editor app in the Notifications system preferences:
I included an Automator workflow service that calls BluetoothConnector from /usr/local/bin
to make it easier to run BluetoothConnector with a keyboard workflow (this is how I’m using it).
To this, you will need a copy of this repo, a brew
install is not enough.
Keyboard > Shortcuts > Services > General
and bind the Bluetooth Connect to whatever you’d like.usr/local/bin
and create a link to your brew
install.
sudo ln -s /opt/homebrew/Cellar/bluetoothconnector/2.1.0/bin/BluetoothConnector
Test out the keyboard shortcut!
You will need to grant permissions the first time you run the workflow.