組み込みProgrammerのチラシの裏

Raspberry Pi Bluetooth Audio Receiver

| Comments

Smart Phoneなどの音楽をBluetooth経由で鳴らしてみませんか? Raspberry piで受信すれば、3.5mmのイヤホンミニジャックがあるため、 外部オーディオに接続することが可能になります。

自宅のコンポ、カーステレオなどをSmart Phoneから操作してみませんか?

Raspberry piにBluetoothは付属していません。 ここではBluetooth USBアダプターを別途使用することを前提としています。

手順

基本

1
2
3
4
5
$ sudo apt-get install bluez pulseaudio-module-bluetooth python-gobject python-gobject-2
$ sudo reboot # ここで一度Rebootする
$ sudo usermod -a -G lp pulse
$ id pulse # lpグループに入っていることを確認
uid=107(pulse) gid=111(pulse) groups=111(pulse),7(lp),29(audio)
/etc/init.d/pulseaudio
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
--- /etc/init.d/pulseaudio.default      2015-01-03 01:19:23.181595965 +0900
+++ /etc/init.d/pulseaudio      2015-01-03 01:21:06.389851720 +0900
@@ -22,7 +22,7 @@

 . /lib/lsb/init-functions

-PULSEAUDIO_SYSTEM_START=0
+PULSEAUDIO_SYSTEM_START=1
 DISALLOW_MODULE_LOADING=1
 test -f /etc/default/pulseaudio && . /etc/default/pulseaudio
 if [ "$PULSEAUDIO_SYSTEM_START" != "1" ]; then
@@ -36,7 +36,8 @@
                mkdir -p $PIDDIR
                chown $DAEMONUSER:$DAEMONUSER $PIDDIR
        fi
-       start-stop-daemon -x $DAEMON -p $PIDFILE --start -- --system --disallow-exit --disallow-module-loading=$DISALLOW_MODULE_LOADING --daemonize --log-target=syslog --high-priority
+       #start-stop-daemon -x $DAEMON -p $PIDFILE --start -- --system --disallow-exit --disallow-module-loading=$DISALLOW_MODULE_LOADING --daemonize --log-target=syslog --high-priority
+       start-stop-daemon -x $DAEMON -p $PIDFILE --start -- --system --disallow-exit --daemonize --log-target=syslog --high-priority
        status=$?
        if [ -e /var/run/pulse/.esd_auth ]; then
                chown pulse:pulse-access /var/run/pulse/.esd_auth
/etc/default/pulseaudio
1
2
3
4
5
6
7
8
9
10
11
--- /etc/default/pulseaudio.default     2015-01-03 02:08:39.625739128 +0900
+++ /etc/default/pulseaudio     2015-01-03 02:04:43.930119610 +0900
@@ -12,7 +12,7 @@
 # is enabled. For other sessions, you can simply start PulseAudio with
 # "pulseaudio --daemonize".
 # 0 = don't start in system mode, 1 = start in system mode
-PULSEAUDIO_SYSTEM_START=0
+PULSEAUDIO_SYSTEM_START=1

 # Prevent users from dynamically loading modules into the PulseAudio sound
 # server. Dynamic module loading enhances the flexibility of the PulseAudio
/etc/pulse/system.pa
1
2
3
4
5
6
7
8
9
10
11
--- /etc/pulse/system.pa.default        2015-01-03 01:22:01.718904246 +0900
+++ /etc/pulse/system.pa        2015-01-03 01:22:39.408255025 +0900
@@ -60,3 +60,8 @@

 ### Enable positioned event sounds
 load-module module-position-event-sounds
+
+.ifexists module-bluetooth-discover.so
+load-module module-bluetooth-discover
+.endif
+
/etc/bluetooth/audio.conf
1
2
3
4
5
6
7
8
9
10
--- /etc/bluetooth/audio.conf.default   2014-12-31 14:50:31.870939097 +0900
+++ /etc/bluetooth/audio.conf   2014-12-31 14:51:07.850367735 +0900
@@ -10,6 +10,7 @@
 # If we want to disable support for specific services
 # Defaults to supporting all implemented services
 #Disable=Control,Source
+Enable=Source,Sink,Headset,Gateway,Control,Media,Socket

 # SCO routing. Either PCM or HCI (in which case audio is routed to/from ALSA)
 # Defaults to HCI

pulse audioの設定を編集します。

/etc/pulse/daemon.conf
1
2
3
4
5
6
7
8
9
--- /etc/pulse/daemon.conf.default      2014-12-31 14:53:39.007967316 +0900
+++ /etc/pulse/daemon.conf      2014-12-31 14:55:17.506403137 +0900
@@ -52,6 +52,7 @@
 ; log-backtrace = 0

 ; resample-method = speex-float-3
+resample-method = trivial
 ; enable-remixing = yes
 ; enable-lfe-remixing = no
/etc/udev/rules.d/99-input.rules
1
2
3
4
5
--- /etc/udev/rules.d/99-input.rules.default    2014-12-31 14:57:16.334516118 +0900
+++ /etc/udev/rules.d/99-input.rules    2014-12-31 14:58:09.243675908 +0900
@@ -1 +1,2 @@
 SUBSYSTEM=="input", GROUP="input", MODE="0660"
+KERNEL=="input[0-9]*",RUN+="/usr/lib/udev/bluetooth"
1
2
$ sudo mkdir /usr/lib/udev
$ sudo vi /usr/lib/udev/bluetooth
/usr/lib/udev/bluetooth
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash

#change if you don't use default audio out
AUDIOSINK="alsa_output.platform-bcm2835_AUD0.0.analog-stereo"


echo "[`date`]" >> /var/log/bluetooth_dev
echo "Executing bluetooth script...|$ACTION|" >> /var/log/bluetooth_dev

ACTION=$(expr "$ACTION" : "\([a-zA-Z]\+\).*")

if [ "$ACTION" = "add" ]
then
   for dev in $(find /sys/devices/virtual/input/ -name input*)
   do
      if [ -f "$dev/name" ]
      then
         mac=$(cat "$dev/name" | sed 's/:/_/g')
         bluez_dev=bluez_source.$mac

         sleep 1

         CONFIRM=`sudo -u pulse pactl list short | grep $bluez_dev`
         if [ ! -z "$CONFIRM" ]
         then
            echo "Setting bluez_source to:  $bluez_dev" >> /var/log/bluetooth_dev
            echo pactl load-module module-loopback source=$bluez_dev sink=$AUDIOSINK rate=44100 adjust_time=0 >> /var/log/bluetooth_dev
            sudo -u pulse pactl load-module module-loopback source=$bluez_dev sink=$AUDIOSINK rate=44100 adjust_time=0 >> /var/log/bluetooth_dev
         fi
      fi
   done
fi

bluetooth-agent

bluetooth-agentを使用して自動でペアリングするサービスを登録します。 GUIでペアリングを行う際には不要となります。

/etc/init.d/bluetooth-agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#! /bin/sh
### BEGIN INIT INFO
# Provides: bluetooth-agent
# Required-Start: $remote_fs $syslog bluetooth pulseaudio
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Makes Bluetooth discoverable and connectable to 0000
# Description: Start Bluetooth-Agent at boot time.
### END INIT INFO
# /etc/init.d/bluetooth-agent
USER=root
HOME=/root
export USER HOME
case "$1" in
        start)
                echo "setting bluetooth discoverable"
                sleep 5 # bluetooth-agentが起動しないことがあるため追加
                sudo hciconfig hci0 pscan
                start-stop-daemon -S -x /usr/bin/bluetooth-agent -b -- 0000
                echo "bluetooth-agent startet pw: 0000"
                ;;
        stop)
                echo "Stopping bluetooth-agent"
                start-stop-daemon -K -x /usr/bin/bluetooth-agent
                ;;
        *)
                echo "Usage: /etc/init.d/bluetooth-agent {start|stop}"
                exit 1
                ;;
esac
exit 0
1
2
3
4
$ sudo chmod 755 /etc/init.d/bluetooth-agent
$ sudo update-rc.d bluetooth-agent defaults
$ amixer cset numid=3 1  # HDMI出力→アナログ出力
$ amixer set Master 100% # 音量を100%

検索時の名前

検索時に見える名前を変えたい場合は以下を参考に設定を変更してください。

/var/lib/bluetooth//config
1
2
3
4
5
6
7
--- /var/lib/bluetooth/<ADDR>/config.default 2014-12-31 15:17:05.606425697 +0900
+++ /var/lib/bluetooth/<ADDR>/config 2014-12-31 15:16:37.906865573 +0900
@@ -1,3 +1,3 @@
-name raspberrypi-0
+name raspberrypi-bluetooth
 pairable yes
 class 0x6e0100
1
2
$ sudo chmod 774 /usr/lib/udev/bluetooth
$ sudo reboot

確認事項

音が出るか確認

1
$ aplay /usr/share/sounds/alsa/Noise.wav

/var/run/pulse …

以下のような起動ログが出ている場合は、pulseaudioをシステム起動していない可能性があります。

/var/log/message
1
2
pulseaudio[2353]: [pulseaudio] authkey.c: Failed to open cookie file '/var/run/pulse/.pulse-cookie': No such file or directory
pulseaudio[2353]: [pulseaudio] authkey.c: Failed to load authorization key '/var/run/pulse/.pulse-cookie': No such file or directory

bluetooth-agentが自動起動しない

1
2
$ ps aux | grep bluetooth
root      2062  0.2  0.4   4748  2204 ?        Ss   16:27   0:00 /usr/sbin/bluetoothd

update-rc.dで登録している時、以下のwarningが出ている可能性があります。 再度/etc/init.d/bluetooth-agentを作り直し、update-rc.dをやり直してください。

1
2
3
4
$ sudo update-rc.d bluetooth-agent defaults
update-rc.d: using dependency based boot sequencing
insserv: warning: script 'K01bluetooth-agent' missing LSB tags and overrides
insserv: warning: script 'bluetooth-agent' missing LSB tags and overrides

Comments