Raspberry Piにbluetoothマイク付きイヤホンを接続してみる


Raspberry PiでGoogle homeみたいなものを作りたいと思い、手元のBluetoothイヤホンを繋げてみます(これが地獄の始まりとは知りませんでした)

この記事 https://qiita.com/homelan/items/e82b919638d3c458d2ec
では

$ sudo bluetoothctl
[bluetooth]# scan on
[bluetooth]# scan off
[bluetooth]# trust MACアドレス
[bluetooth]# pair MACアドレス
[bluetooth]# connect MACアドレス
[MR230]# paired-devices
[MR230]# quit

とするよう書かれていますが、connectを発行したところ、

[bluetooth]# connect MACアドレス
Attempting to connect to MACアドレス
Failed to connect: org.bluez.Error.Failed

となってしまい、接続に失敗してしまいました。次に調べて出てきたサイトでは、 systemctl status bluetooth を見ろと書いてあったので実行すると、

12月 05 17:18:28 raspberrypi bluetoothd[720]: a2dp-sink profile connect failed for MACアドレス Protocol not available

と出てきました。うーん。a2dp-sinkがうまくいっていないみたいです。

この記事の中間あたりにそれっぽいことが書いてあるので試してみます。

$sudo vi /lib/systemd/system/bluealsa.service
[Unit]
Description=BluezALSA proxy
Requires=bluetooth.service
After=bluetooth.service
[Service]
Type=simple
User=root
ExecStart=/usr/bin/bluealsa -p a2dp-sink

$ sudo systemctl restart bluealsa.service
$ sudo vi /etc/systemd/system/bluealsa-aplay.service
[Unit]
Description=BlueALSA aplay service
After=bluetooth.service
Requires=bluetooth.service

[Service]
ExecStart=/usr/bin/bluealsa-aplay 00:00:00:00:00:00

[Install]
WantedBy=multi-user.target

$ sudo systemctl enable bluealsa-aplay.service
$ sudo systemctl start bluealsa-aplay.service

ここまでは無事に進み、手元のスマホからRaspberry Piが見えましたが、相変わらずBluetoothイヤホンとは繋がりません…。

お次の記事はこちら。Raspberry PiでBluetoothスピーカーが使えるのか格安スピーカー実践で検証してみる。おっと、この記事と全く同じ状況ではないですか。ということで、

sudo apt-get install pi-bluetooth blueman
sudo apt-get install pulseaudio pavucontrol pulseaudio-module-bluetooth

そして再起動!

ええ、もちろんダメでした。よく見てみると、Audio sinkがbluetoothctl でshowしても出てきません…。もう一度さっきの手順をしてみると、bluealsaとpulseaudio-module-bluetoothは排他の関係にあるのか、どちらかしかインストールできないみたいです。こうなったらpulseaudio-module-bluetoothルートでAudio sinkを有効にしてみましょう。

今度はこの記事! RaspberryPi 3B+/Raspbian LiteでBluetoothオーディオレシーバー作成

mpdをインストールし、/etc/mpd.conf で

 #audio_output  {
#   type        "alsa"
#   name        "My ALSA Device"
#   device      "hw:0,0"    # optional
#   mixer_type      "hardware"      # optional
#   mixer_device    "default"   # optional
#   mixer_control   "PCM"       # optional
#   mixer_index "0"     # optional
#}
audio_output {
   type        "pulse"
   name        "My Pulse Output"
#   server      "remote_server"     # optional
#   sink        "remote_server_sink"    # optional
}

そして

# usermod -G bluetooth mpd
# systemctl enable mpd
bluetoothのコントローラーにaudio-sinkを追加するための権限をbluetoothグループに渡す
/etc/dbus-1/system.d/bluetooth.conf
bluetoothのpolicyにorg.bluez.MediaPlayer1の行を追加
<policy group="bluetooth">
<allow send_destination="org.bluez"/>
<allow send_interface="org.bluez.MediaPlayer1"/>
</policy>

/etc/bluetooth/audio.conf
デフォルトだとファイルがないので新規作成してA2DP関連のProfileを使えるように設定
[General]
Enable=Source,Sink,Media,Socket

と書いてあるように行ったところ、mpdサービスが立ち上がらない!? と思ったら}のコメントアウトを忘れていました。今度はpulseaudioが動いていないのでさらにジャンプ。

次はこの記事。Raspberry piでbluetoothスピーカーを鳴らす

systemdで制御できるようにサービスを定義します。
$ sudo vi /etc/systemd/system/pulseaudio.service
[Unit]
Description=Pulse Audio
[Service]
Type=simple
ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disable-shm
[Install]
WantedBy=multi-user.target

configファイルの修正
bluezプロファイルをを使用するための設定とbluetooth用モジュールの読み込み設定を追加します。 その後一度再起動します。
$ sudo vi /etc/dbus-1/system.d/pulseaudio-bluetooth.conf
<busconfig>
<policy user="pulse">
<allow send_destination="org.bluez"/>
</policy>
</busconfig>
$ sudo vi /etc/pulse/system.pa
### Automatically load driver modules for Bluetooth hardware
.ifexists module-bluetooth-policy.so
load-module module-bluetooth-policy
.endif
.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover
.endif
$ sudo reboot
pulseaudioサービスの起動
先ほど定義したpulseaudioサービスを開始し、自動起動するようにします。
$ sudo systemctl start pulseaudio.service
$ sudo systemctl status pulseaudio.service
$ sudo systemctl enable pulseaudio.service

これで何とかAudio sinkが出てきたものの、まだ音が出ない…。rootになってやけくそで音が出るコマンドがあるみたいなのでそれを入力

speaker-test -c2 -twav

?!?! なんかイヤホンからきこえるんですけどおおおおおお !?!?!?

知らない間にrootで音声が出たらしいけど、ユーザーに戻ると途端に沈黙・・・。alsamixerコマンドで2者の違いを見てみても全く同じ、、。 そんなときの記事はこちら。Debian Busterのrootでないと音が出ない

これを見てみると、権限が誤ってる、などが書いてある。権限…rootでやったり適当にファイル書いてたからなあ…。もしかしたらユーザーにread権限がないからかも!と思い、 ps aux | grep pulseと調べて実行プロセスを見ると、やはりユーザーとrootで違う!

作ったファイルを順番にls -lで権限を確認していくと、ありました。/etc/mpd.confがユーザーにread権限がありません!

そして権限を渡すことによって、無事に音声が聞こえてきました。これをやるだけで丸一日かかりました…(ただいま夜10時)

環境構築が一番大変なんだなと思いました。

追記

再起動したらちゃんと音が出なくなりました。どうやらユーザーでもpluseaudioを動かしているかららしく、rootグループに間違って入ったユーザーを消去し、ワンモア再起動です。

うーん、どうやらpulseaudioがユーザーでも立ち上がり、しかも悪いことにdaemonize=no オプションで立ち上がってるせいで、一度pulseaudio --kill -> pulseaudio --startってやらないといけないみたいです…

最後に参照するのはこちら。Raspberry Piでプログラムを自動起動する5種類の方法を比較・解説。ここから、autostartの設定をもらいます。

$ mkdir -p ~/.config/lxsession/LXDE-pi
$ cp /etc/xdg/lxsession/LXDE-pi/autostart ~/.config/lxsession/LXDE-pi/

このあとに実行したいコマンド名を書くらしく、pluseaudio -kとpulseaudio --startを追記しました。

この記事が気に入ったらサポートをしてみませんか?