Raspberry piでbluetoothスピーカーを鳴らす
Raspberry pi zero wはbluetoothにも対応しているので、スピーカーを繋いで音を鳴らしてみます。
構成は以下の通り。
- Raspberry pi zero w
- Rasbian Stretch
- Amazonベーシック ポータブルBluetoothスピーカー
スピーカーは今は売ってないんですね。。。 小さくて安く買える良品だったのに、残念。
必要なパッケージをインストールする
bluetoothで音を鳴らすために必要なパッケージを導入します。
$ sudo apt install pulseaudio pulseaudio-module-bluetooth
Pulse Audioサービスを作成する。
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
bluetoothデバイスを接続
bluetoothctlコマンドでデバイスと接続し、ペアリングします。
scanで見つかったアドレスに対してpairとtrust、connectを行います。
$ sudo bluetoothctl [NEW] Controller **:**:**:**:**:** rasizw-01.iot.local [default] [bluetooth]# agent on Agent registered [bluetooth]# default-agent Default agent request successful [bluetooth]# scan on Discovery started [CHG] Controller **:**:**:**:**:** Discovering: yes [NEW] Device **:**:**:**:**:** Amazon_BT4 [bluetooth]# pair **:**:**:**:**:** Attempting to pair with **:**:**:**:**:** [CHG] Device **:**:**:**:**:** Connected: yes [CHG] Device **:**:**:**:**:** UUIDs: 0000110b-0000-1000-8000-00805f9b34fb [CHG] Device **:**:**:**:**:** UUIDs: 0000110c-0000-1000-8000-00805f9b34fb [CHG] Device **:**:**:**:**:** UUIDs: 0000110e-0000-1000-8000-00805f9b34fb [CHG] Device **:**:**:**:**:** ServicesResolved: yes [CHG] Device **:**:**:**:**:** Paired: yes Pairing successful [Amazon_BT4]# trust **:**:**:**:**:** [CHG] Device **:**:**:**:**:** Trusted: yes Changing **:**:**:**:**:** trust succeeded [CHG] Device **:**:**:**:**:** ServicesResolved: no [CHG] Device **:**:**:**:**:** Connected: no [bluetooth]# connect **:**:**:**:**:** Attempting to connect to **:**:**:**:**:** [CHG] Device **:**:**:**:**:** Connected: yes Connection successful [CHG] Device **:**:**:**:**:** ServicesResolved: yes [CHG] Device **:**:**:**:**:** UUIDs: 00001105-0000-1000-8000-00805f9b34fb [CHG] Device **:**:**:**:**:** UUIDs: 0000110a-0000-1000-8000-00805f9b34fb [CHG] Device **:**:**:**:**:** UUIDs: 0000110c-0000-1000-8000-00805f9b34fb [CHG] Device **:**:**:**:**:** UUIDs: 0000110e-0000-1000-8000-00805f9b34fb [CHG] Device **:**:**:**:**:** UUIDs: 00001112-0000-1000-8000-00805f9b34fb [CHG] Device **:**:**:**:**:** UUIDs: 00001200-0000-1000-8000-00805f9b34fb [Amazon_BT4]# quit [DEL] Controller **:**:**:**:**:** rasizw-01.iot.local [default] $
グループへのユーザ追加
デフォルトではrootであっても音を鳴らすことができないため、グループに追加します。
$ sudo usermod -a -G pulse-access,audio root
音を鳴らす
aplayコマンドでも音は鳴るんですが、気づいたら音が鳴らなくなっていることがあるのでpaplayコマンドで鳴らしたほうがいいようです。
$ sudo paplay nc52679.wav