Raspberry Pi Zero WHにBluetoothスピーカーを接続する
手元にBluetoothスピーカーがあったので Raspberry Pi Zero WHと接続して音を出してみます。
スピーカー: Anker SoundCore mini
こちらを参考にしています。
Bluetooth audio with BlueZ 5 · Issue #198 · ev3dev/ev3dev · GitHub
PulseAudio の設定
まず必要なパッケージをインストールします。
$ sudo apt-get -y install pulseaudio $ sudo apt-get -y install pulseaudio-module-bluetooth
次にsystemdサービス化をします。
以下の2つのファイルを作成し、設定を記載してください。
systemdサービス化
$ sudo nano /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
サービスポリシー設定
$ sudo nano /etc/dbus-1/system.d/pulseaudio-bluetooth.conf
<busconfig> <policy user="pulse"> <allow send_destination="org.bluez"/> </policy> </busconfig>
モジュール設定
以下のファイルの最後の行に追記してください。
$ sudo nano /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
次にサービスを開始して自動起動するようにします。
$ sudo systemctl start pulseaudio.service $ sudo systemctl enable pulseaudio.service
この時点でスピーカーは繋がっていませんがwavが再生できるかテストしてみます。
$ paplay /usr/share/sounds/alsa/Front_Center.wav Connection failure: Access denied
エラーが出る場合は権限がないので追加します。
(例:ユーザー名 reccoo)
$ sudo gpasswd -a reccoo pulse-access
Bluetoothスピーカーの接続
スピーカーの電源を入れて、Bluetooth接続待ち状態にします。
bluetoothctlを使ってスピーカーとペアリングします。
$ sudo bluetoothctl
デバイスを探してアドレスを得ます。
[bluetooth]# scan on [NEW] Device **:**:**:**:**:** SoundCore mini [bluetooth]# scan off
ペアリングして接続します。
[bluetooth]# pair **:**:**:**:**:** Pairing successful [bluetooth]# connect **:**:**:**:**:** Connection successful
次回以降自動で接続するようにします。
[SoundCore mini]# trust **:**:**:**:**:** Changing **:**:**:**:**:** trust succeeded
bluetoothctlから抜けます。
[SoundCore mini]# quit
これで接続は終わりです。
先ほど再生したファイルが再度再生できるか確認します。
$ paplay /usr/share/sounds/alsa/Front_Center.wav