AirPodsをMacに強制的に接続する

AirPods Proの自動切り替え機能、MacからiPhoneへの切り替えは便利なんだけど、逆はうまく行かないことが多い。というかMacに繋げてたはずなのにちょっとiPhone弄った時に切り替わってていざミーティングしようと思ったらMacに繋がってなかったりとかして焦る。

で、この前 Rebuild.fm 聞いてたら id:miyagawa が「Alfredで接続後に音を鳴らすようにしてる」って言っててそれだ!!って思ったので真似てみた。いやマジでこの最後に音を鳴らすというのがメッチャ頭良いというかナイスハックで自分の耳で繋がってることが確認できるので素晴らしい!!

 やること

  1. bluetoothAirPodsに接続
    1. トグルとかにすると逆にわかりにくいので接続のみ
  2. MacサウンドAirPodsにする
    1. たまに繋がってるのに音が聞こえないことがあるのは別のスピーカとかになってる
  3. 音を鳴らす
    1. 自分の耳で準備が出来たことを確認できるので確実

という感じのことをします。

Install BluetoothConnector

Install BluetoothConnector .

コマンドラインからBluetooth接続弄れるやつを入れる

$ brew install bluetoothconnector

AirPodsMAC addressを調べる

これ簡単でさっきインストールしたbluetoothconnectorを引数無しで実行すると一覧が表示される

$ bluetoothconnector
Error:
MAC Address missing. Get the MAC address from the list below (if your device is missing, pair it with your computer first):
f6-ad-d5-d0-c2-d1 - MX Ergo
fc-66-cf-6a-52-d1 - iPhone
4c-ef-c0-a6-52-b1 - Echo-FHW
34-df-2a-4a-a2-41 - AKG Y50BT
3c-bf-60-6c-62-32 - iPad
f0-5c-d5-e5-d2-42 - Yoshiori’s AirPods Pro

Usage: BluetoothConnector [--connect] [--disconnect] [--status] [--notify] [<MAC address>]
  See 'BluetoothConnector --help' for more information.

例えば俺のAirPodsMAC addressは f0-5c-d5-e5-d2-42

Install SwitchAudioSource

Install SwitchAudioSource .

コマンドラインからMacのオーディオ設定弄れるやつを入れる

$ brew install switchaudio-osx

AirPodsの名前を調べる

さっきの一覧にも出てたんだけど一応 SwitchAudioSourceコマンドでも確認してみる

$  SwitchAudioSource -a -t output 
DELL U2720Q
ATR2100x-USB Microphone
MacBook Proのスピーカー
Yoshiori’s AirPods Pro

シェルスクリプト用意

#!/bin/bash
AIR_PODS_ADDRESS=f0-5c-d5-e5-d2-42 # Your AirPods MAC address
AIR_PODS_NAME="Yoshiori’s AirPods Pro" # Your AirPods name

/usr/local/bin/bluetoothconnector -c $AIR_PODS_ADDRESS
for ((i=0 ; i<10 ; i++))
do
    if [ "Connected" == $(/usr/local/bin/bluetoothconnector  -s $AIR_PODS_ADDRESS) ]; then
        sleep 1
        /usr/local/Cellar/switchaudio-osx/1.1.0/SwitchAudioSource -s  $AIR_PODS_NAME
        sleep 1
        say -v samantha Connected
        break
    fi
    sleep 1
done

AIR_PODS_ADDRESSAIR_PODS_NAMEを自分のに書き換えて実行すると強制的に実行したMacに接続するようになる。

Alfredに登録

f:id:Yoshiori:20210413171228p:plain

最高便利! ちなみにオリジナルはafplanyで音を鳴らしてる。(最初sayに違和感あったんだけどsamanthaに喋らせるようにしたらそれで良くなっちゃった。)