This thing drove me crazy for years. I'm using my AirPods only with my iPhone and larger headphones with Mac and never want it to switch back and forth, but it always does.
I might have just solved it with AirPods 4. It works under the latest macOS at the moment (15.3).
I looked in all possible places where settings can be stored and found something interesting, and it likely works.
Steps:
1 - Connect AirPods to Mac, go to Settings, select "When last connected to Mac".
2 - Open Terminal, run the following command, you will be asked for a password.
sudo defaults read /Library/Bluetooth/Library/Preferences/com.apple.MobileBluetooth.devices
The output will look something like that:
{
... different stuff ...
"FC:A5:C8:B8:07:67" = {
... different stuff ...
OwnerNoMagicPairing = 0;
... different stuff ...
UserNameKey = "My AirPods 4";
... different stuff ...
};
... different stuff ...
}
You are looking for the section with your AirPods name in UserNameKey; it should (but probably might not) have the OwnerNoMagicPairing key set to 0.
Next, you need to copy the top-level key of this section ("FC:A5:C8:B8:07:67" in case above).
3 - Run the following command, replacing "FC:A5:C8:B8:07:67" with your specific value from above.
sudo defaults write /Library/Bluetooth/Library/Preferences/com.apple.MobileBluetooth.devices "FC:A5:C8:B8:07:67" -dict-add OwnerNoMagicPairing -int 1
You may rerun the first command to check that now OwnerNoMagicPairing set to 1.
4 - Open AirPods settings again, check that "When last connected to Mac” is still selected, and click disconnect.
After that, when I insert my AirPods, it connects to my iPhone.
On Mac, click connect on AirPods, and it switches to Mac.
Put AirPods back to the case, take them out, and insert them again - they still connect to my iPhone, even though they were last connected to Mac and even if my iPhone screen is off.
Play anything on Mac and AirPods doesn’t switch to it automatically.
Fn fantastic!
My AirPods 2, which I no longer use but still added to my settings, also have this key. So this should work with other Apple headphones too.