I am struggling to find some (elegant) way to connect my Mac to a (previously paired) Bluetooth device via terminal.
There is a solution using Applescript simulating mouse clicks, but it has some major issues:
- Bluetooth menu needs to be visible in the Menu bar
- Terminal needs access to the Accessibility Controls
- Does not work reliably
- It is slow as hell
- When you use the mouse while the script is running, it fails.
So, this is more or less a no-go..
I don't need to switch Bluetooth on and off in general, I don't need any pairing mechanism, I just need some way to trigger the connection to known devices via terminal.
My use case is a Bluetooth Sound Module – for example I called it "NoiseBox". What I want is this:
./magic-script.sh connect NoiseBox
or
./magic-script.sh disconnect NoiseBox
Can you help me?
PS: Is it possible to write some small Application in Swift or ObjC which does this?
Best Answer
As there are some upvotes and many views, I will answer my own question there:
Yes, it is possible to write some small application in Swift or ObjC which does this. And some people did that already:
BluetoothConnector
is written in Swift, which does (currently) only support connecting and disconnecting (It seems to be pretty new, so more features may follow some day).blueutil
is written in Objective-C. In addition to (dis-)connecting it can also toggle power, discoverability, show if you are already connected, etc.I tried both, and both worked well, currently I'll stick with
blueutil
- it has more features, which come in handy in shell scripts.