Automating Switching AirPods Between Devices

I got my AirPods Pro in 2019 for their sound quality, noise cancellation, and headset capability. I never imagined they’d become as indispensable as they have since I started working from home.

 

As convenient as they are in most setups, I have a less-common use case. I connect them regularly to a client-supplied Mac that isn’t signed in to my iCloud account. AirPods Pro still work great in this scenario, but I was able to improve matters even more.

The Setup

Like most AirPods owners, I paired my AirPods Pro to my iPhone. Because my iPhone, iPad, Apple TV, and MacBook Pro are all connected to the same iCloud account, they share pairing information and can at any time automatically “pull” the AirPods from wherever they’re not being used.

In addition to this pairing, I frequently need to work and converse with a group of clients using the Mac they supplied. I’ve used the AirPods’ manual pairing functionality to pair them there too. They stay paired to all of these devices.

This works fine, but whenever I want to switch between the client Mac and any of my own devices, I have to use the Bluetooth menu or the AirPlay selection menu. The latter, in particular, can be pretty awkward to pull up, but I did it anyway.

A Shortcut

A little while ago, I came across an iOS shortcut to pull AirPods to an iOS device. It inspired me to smooth this workflow.

I made my own version, which is otherwise identical but has one key change: audible feedback. You can create your own in Apple’s Shortcuts app.

A screenshot of my "Connect AirPods" shortcut, described below.

This workflow has three steps:

  1. A Bluetooth step: Turn Bluetooth on.
  2. An AirPlay step: Set playback destination to “Mattie’s AirPods Pro.”
  3. A Documents step: Speak “Connected.”

These three steps will pull my AirPods from any other device — one of my own, or the client’s — to the device running the shortcut.

When I run this shortcut, I’ll usually hear the Bluetooth connection tone when it succeeds, but not in all circumstances. That’s why I added the audible feedback. Now I know that when I hear the word “connected,” I’m ready to go on my iOS device.

With a working shortcut in hand, I could add it to the home screen. I chose to just tell Siri to “Connect AirPods” whenever I wanted to pull them. This is as simple as holding a button on the device in question and was the most efficient way I could find.

The iOS half of this equation was definitely the easy one.

An Automation

Shortcuts don’t exist on macOS, but macOS has had Automator for many years. I started looking around for options there.

There were a few AppleScripts out there that purported to get the job done, but they were broken on Big Sur, and fixing them was frustrating. They also needed to open System Preferences and select my AirPods to get the job done.

I looked around a bit more and found two command-line tools available in Homebrew: BluetoothConnector and switchaudio-osx.

In Automator, I created a Quick Action that runs this shell script:

set -e

mac=e8-85-4b-59-a2-72
name="Mattie’s AirPods Pro"

/usr/local/bin/BluetoothConnector --connect "${mac}" 1>&2
while [ `/usr/local/bin/BluetoothConnector --status "${mac}"` != Connected ]
do
	sleep 1
done

until /usr/local/bin/SwitchAudioSource -a | grep "${name}"
do
	sleep 1
done

/usr/local/bin/SwitchAudioSource -s "${name}" 1>&2
/usr/local/bin/SwitchAudioSource -s "${name}" -t input 1>&2
until /usr/local/bin/SwitchAudioSource -c | grep "${name}"
do
	sleep 1
done
until /usr/local/bin/SwitchAudioSource -c -t input | grep "${name}"
do
	sleep 1
done

/usr/bin/say "Connected."

This does the following:

  1. Asks BluetoothConnector to make a connection to my AirPods Pro’s MAC address; that’s the sequence starting with e8-85-4b. (You can get yours on a Mac that has paired with your AirPods by typing BluetoothConnector at a shell prompt.)
  2. Polls every second until the AirPods are connected.
  3. Polls every second until the AirPods are an available sound output.
  4. Asks SwitchAudioSource to switch the Mac’s output and input to the AirPods. Note: watch out for the quote character here! I recommend copying and pasting your device name from BluetoothConnector output because iOS will set up your device’s name with a Unicode apostrophe (), not the typewriter apostrophe (') that you likely usually use.
  5. Polls every second until the AirPods are both output, then the same for input.
  6. Uses say to give us that same audible feedback once everything is done.

I configured this Quick Action (at the top of the Automator window) to receive “no input” in “any application” and saved it. This makes it widely and easily available.

Next, I set up a shortcut key for it in System Preferences -> Keyboard -> Shortcuts -> Services. (Scroll all the way to the bottom, and you’ll find your Quick Actions.)

The Keyboard preferences pane, showing the Shortcut panel, and a "Connect AirPods" Quick Action highlighted under Services.

I also tried to set it up on my Touch Bar, but despite setting it up in System Preferences -> Extensions -> Touch Bar, it didn’t appear when I set my Touch Bar to “Quick Actions.” (If you have a solution, please let me know!)

It does appear using the Quick Actions control strip button, so that’s good. But I very much prefer using the keyboard shortcut.

Now, whenever I press option-command-A, that Mac will pull my AirPods from wherever they are, and I’ll know I’m ready to go when I hear “Connected” in my ears.

Parting Thoughts

I’ve found this process a great improvement over pulling down menus on macOS and endless taps on iOS. I don’t even really have to think about using my AirPods with one of my devices — I hit a key combo or say two words to Siri, and it’s ready to go.

It’s funny to think that I originally sought AirPods Pro out because I wanted portable isolation. I didn’t realize they’d also make it much easier to hear people with poor audio setups and to hear despite my own mild hearing loss thanks to Hearing Accommodations on iOS 14.

In short, these little buds have made working remotely much better.

 
Conversation
  • Roman says:

    Check Tooth Fairy app. If you can install apps on the supplied computer that is.

  • Phil says:

    This makes connecting my AirPods to my MacBook Pro much easier. Thank you for sharing this, Mattie!

  • PeeWee says:

    Hi, sounds good, thanks, tried it, but it says:

    „zsh:6: no such file or directory: /usr/local/bin/BluetoothConnector“

    Did something change in macOS 12 ?

  • Comments are closed.