Orangepi PCでInternet Radioを構築する

Bluetoothスピーカ


今までUSB接続のスピーカーを使ってきましたが、mpdの出力先をBluetoothスピーカに変更します。
色々なページでmpdの出力先をBluetoothスピーカに変更する手順が公開されていますが、
こ ちらこ ちらこ ちらこ ちら参考にして、以下の手順でBluetoothスピーカを使うことができました。

/etc/pulse/default.pa の変更については、どの資料でも同じですが、usermodで追加するグループ名がページによってまちまちで、
どのグループ名が正解か、良く分かっていません。

以下の手順はArmbianでもRaspbianでも同じです。

【パッケージのインストール】

必要なパッケージをインストールして、pulseaudioを開始します。
$ sudo apt install pulseaudio pulseaudio-module-bluetooth bluetooth

$ sudo usermod -G bluetooth -a orangepi → 下線部分はログイ ンユーザ名

$ sudo vi /etc/pulse/default.pa
以下の1行を追加します
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1

$ pulseaudio --start

【ペアリング】

今回はTG-117という型番のBluetoothスピーカとペアリングしました。
下線部分が入力です。
$ bluetoothctl
[NEW] Controller 00:1B:DC:03:D3:E5 raspberrypi [default]

[bluetooth]# power on
Changing power on succeeded

[bluetooth]# discoverable on
Changing discoverable on succeeded
[CHG] Controller 00:1B:DC:03:D3:E5 Discoverable: yes

[bluetooth]# pairable on
Changing pairable on succeeded

[bluetooth]# agent on
Agent registered

[bluetooth]# default-agent
Default agent request successful

ここでBluetoothスピーカーをペアリング待ちにします。

[bluetooth]# scan on
Discovery started
[CHG] Controller 00:1B:DC:03:D3:E5 Discovering: yes
[NEW] Device 54:14:8E:38:A8:28 TG-117
[CHG] Device 54:14:8E:38:A8:28 RSSI: -70
[CHG] Device 54:14:8E:38:A8:28 RSSI: -39

[bluetooth]# scan off
[CHG] Device 54:14:8E:38:A8:28 RSSI is nil
Discovery stopped
[CHG] Controller 00:1B:DC:03:D3:E5 Discovering: no

[bluetooth]# pair 54:14:8E:38:A8:28
Attempting to pair with 54:14:8E:38:A8:28
[CHG] Device 54:14:8E:38:A8:28 Connected: yes
[CHG] Device 54:14:8E:38:A8:28 UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device 54:14:8E:38:A8:28 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device 54:14:8E:38:A8:28 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device 54:14:8E:38:A8:28 UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device 54:14:8E:38:A8:28 ServicesResolved: yes
[CHG] Device 54:14:8E:38:A8:28 Paired: yes
Pairing successful
[CHG] Device 54:14:8E:38:A8:28 ServicesResolved: no
[CHG] Device 54:14:8E:38:A8:28 Connected: no

[bluetooth]# trust 54:14:8E:38:A8:28
[CHG] Device 54:14:8E:38:A8:28 Trusted: yes
Changing 54:14:8E:38:A8:28 trust succeeded

[bluetooth]# connect 54:14:8E:38:A8:28
Attempting to connect to 54:14:8E:38:A8:28
[CHG] Device 54:14:8E:38:A8:28 Connected: yes
Connection successful
[CHG] Device 54:14:8E:38:A8:28 ServicesResolved: yes

Bluetoothスピーカーがピポと鳴り、接続できたことを知らせてくれます。

 [TG-117]# quit
Agent unregistered
[DEL] Controller 00:1B:DC:03:D3:E5 orangepipc [default]

【mpd.confの変更】

出力先をPulse Deviceに変更します。
$ sudo vi /etc/mpd.conf

audio_output {
        type            "pulse"
        name            "My Pulse Device"
        server          "127.0.0.1"
        mixer_type      "software"      # optional

以下では動きません
audio_output {
        type            "pulse"
        name            "My ALSA Device"
#       server          "127.0.0.1"
        server          "localhost"
        mixer_type      "software"      # optional

mpdを再起動しplayコマンドを実行します。
設定が正しければBluetoothスピーカーから音が出ます。
$ sudo service mpd restart

$ mpc play
Left Coast 70s: Mellow album rock from the Seventies. Yacht friendly. [SomaFM]: Jackson Browne - The Fuse
[playing] #2/2   0:00/0:00 (0%)
volume: 50%   repeat: off   random: off   single: off   consume: off

【システム起動時】

システム起動時は以下のコマンドだけでBluetoothスピーカーとコネクトできます。
$ pulseaudio --start

$ bluetoothctl
[NEW] Controller 00:1B:DC:03:D3:E5 orangepipc [default]
[NEW] Device 54:14:8E:38:A8:28 TG-117

[bluetooth]# connect 54:14:8E:38:A8:28
Attempting to connect to 54:14:8E:38:A8:28
[CHG] Device 54:14:8E:38:A8:28 Connected: yes
Connection successful
[CHG] Device 54:14:8E:38:A8:28 ServicesResolved: yes

[TG-117]# quit
[DEL] Controller 00:1B:DC:03:D3:E5 orangepipc [default]

$ mpc play
Left Coast 70s: Mellow album rock from the Seventies. Yacht friendly. [SomaFM]: Jackson Browne - The Fuse
[playing] #2/2   0:00/0:00 (0%)
volume: 50%   repeat: off   random: off   single: off   consume: off

【システム起動時の自動接続】

システム起動時にログインすることなく、自動的にBluetoothデバイスに接続する手順ですが、こちらによると、
以下を設定すると自動接続すると書かれていますが、私の環境では自動接続できませんでした。
おそらく、システム起動時にpulseaudioが起動していないためと思います。
$ sudo vi /etc/bluetooth/main.conf

[Policy]
AutoEnable=true

ログイン後にシェルからpulseaudioを起動して、bluetoothctlでconnectを実行すると必ず接続できます。
そこで、かなり強引ですが以下のスクリプト(autoConnect.sh)を作りました。
#!/bin/bash
sudo -u ログインユーザ名 pulseaudio --start

sudo -u ログインユーザ名 bluetoothctl << EOF
connect 54:14:8E:38:A8:28
EOF

システム起動と同時にスクリプトを起動するやり方はいくつかありますが、今回もcrontabに任せることにします。
このスクリプト(autoConnect.sh)をcrontabに登録します。
システムを再起動するとBluetoothスピーカーがピポと鳴り、接続できたことを知らせてくれます。
@reboot sleep 15 && /home/orangepi/autoConnect.sh



JBL CHAGER3を入手しましたが、LinuxのBluetoothドライバーとの相性がメチャクチャ悪く、音が飛びます。
音質は確かに申し分ないですが、音飛びが気になって使い物になりません。
仕方が無いので、安物のチャイナBluetoothスピーカを購入して使っています。

多分...おしまい