2014年7月27日 星期日

使用 raspberry Pi 當藍牙喇吧 Bluetooth audio streaming


Bluetooth audio streaming

IPAD 測試過ok

測試過程中如果發生不正常動作請查看系統日誌
nano  /var/log/syslog

按CTRL-G 可查看NANO現在操作說明
^X 的^ 表按下CTRL在加按X
^X :離開檔案編輯
M-/ 的M表按下ALT在加按/
M-/ :跳到檔案最後一行

參考:
http://www.raspberrypi.org/forums/viewtopic.php?f=38&t=68779&p=584129&hilit=Bluetooth+audio+streaming#p584129

1. Install required packages
sudo apt-get update
sudo apt-get install bluez pulseaudio-module-bluetooth python-gobject python-gobject-2

2. Change audio default settings 
sudo usermod -a -G lp pi
sudo nano /etc/bluetooth/audio.conf
Add/Complete the following line in the [General] section :
改變內容為下
Enable=Source,Sink,Media,Socket

sudo nano /etc/pulse/daemon.conf
改變內容為下
resample-method = trivial

sudo reboot

3. Make sure your bluetooth adapter is working
確認藍芽設備有工作
sudo hciconfig -a
You can see the bluetooth address of your USB adapter on the line BD Address: XX:XX:XX:XX:XX:XX
可以改變PI 的藍芽名稱
sudo nano /var/lib/bluetooth/XX:XX:XX:XX:XX:XX/config
sudo nano /etc/bluetooth/main.conf
You can use some other useful bluetooth commands to make sure it is working with your phone (or other device)

搜尋手機(記得打開可搜尋)
sudo hcitool scan
You should see your phone with its bluetooth address under the form XX:XX:XX:XX:XX:XX
ping 設備
sudo l2ping XX:XX:XX:XX:XX:XX
連線測試
sudo hcitool cc XX:XX:XX:XX:XX:XX

4. Pair your phone and your raspberry
sudo hciconfig hci0 piscan
sudo hciconfig -a
會看見 UP RUNNING PSCAN ISCAN
開始配對  bluez-simple-agent
A "Enter Pin :" message should appear on your raspberry asking you for the PIN code : enter the same code you just defined on your phone and press enter. If you try to connect your phone without stopping the bluez-simple-agent, you should be asked to authorize the connection (message "Authorize connection (yes/no):").
Your phone is now paired. You can stop the bluez service by pressing Ctrl+C.
按CTRL-C 離開
以上配對僅短暫使用
要記錄在PI 中需使用下列指令
bluez-test-device trusted XX:XX:XX:XX:XX:XX yes

5. Connect the bluetooth source to the default sink
當連線後
pactl list sources short
確認出現
0    alsa_output.platform-bcm2835_AUD0.0.analog-stereo.monitor    module-alsa-card.c    s16le 2ch 44100Hz    SUSPENDED
1    bluez_source.XX_XX_XX_XX_XX_XX    module-bluetooth-device.c    s16le 2ch 44100Hz    SUSPENDED

pactl list sinks short
確認出現
0    alsa_output.platform-bcm2835_AUD0.0.analog-stereo    module-alsa-card.c    s16le 2ch 44100Hz    SUSPENDED

輸入下面指令 (一定要輸入 不然沒有聲音,XX_XX_XX_XX_XX_XX表設備位置)
pactl load-module module-loopback source=bluez_source.XX_XX_XX_XX_XX_XX sink=alsa_output.platform-bcm2835_AUD0.0.analog-stereo

6. Change the sound settings
Change the default HDMI sound ouput for the jack output : (改變原來聲音預設輸出HDMI 改為JACK 輸出
amixer cset numid=3 1
調整聲音到最大聲
amixer set Master 100%
pacmd set-sink-volume 0 65537

執行到這裡 請確定有聲音輸出才執行下一步


7. Configure pulse-audio in system mode (necessary to launch pulse without being logged on the raspberry)
sudo nano /etc/default/pulseaudio
改變內容為下
PULSEAUDIO_SYSTEM_START=1
DISALLOW_MODULE_LOADING=0

加入使用者
sudo adduser pi pulse-access
sudo nano /etc/pulse/client.conf
改變內容為下
autospawn = no
sudo nano /etc/pulse/daemon.conf
改變內容為下
allow-module-loading = yes
load-default-script-file = yes
default-script-file = /etc/pulse/default.pa

sudo nano /etc/dbus-1/system.d/pulseaudio-system.conf
改變內容為下
<policy user="pulse">
      <allow own="org.pulseaudio.Server"/>
      <allow send_destination="org.bluez"/>
      <allow send_interface="org.bluez.Manager"/>
</policy>

8. Create a job that looks for bluetooth connections
mkdir /home/pi/pulseAudioBluetooth
nano /home/pi/pulseAudioBluetooth/checkForBluetoothDevice.sh
新增內容如下
#!/bin/bash

#This script looks for a bluetooth audio source and launch the pulse audio loopback module if necessary

#log traces :
#date=$(date "+%Hh%Mm%Ss")
#echo "Script Exec "$date

bluetoothSource=$(pactl list sources short | grep bluez_source)

read loopbackStatus < /home/pi/pulseAudioBluetooth/bluetoothLoopbackStatus.txt

if [[ $bluetoothSource != "" ]] && [[ $loopbackStatus == "0" ]]
then
   source=${bluetoothSource:2:30}
   pactl load-module module-loopback source=$source sink=alsa_output.platform-bcm2835_AUD0.0.analog-stereo
   echo "1" > /home/pi/pulseAudioBluetooth/bluetoothLoopbackStatus.txt
   #echo "command launched"

else
   if [[ $bluetoothSource == "" ]] && [[ $loopbackStatus == "1" ]]
   then
      echo "0" > /home/pi/pulseAudioBluetooth/bluetoothLoopbackStatus.txt
      #echo "reset loopbackStatus"
   fi
fi

sudo chmod u+x /home/pi/pulseAudioBluetooth/checkForBluetoothDevice.sh
echo 0 > /home/pi/pulseAudioBluetooth/bluetoothLoopbackStatus.txt

9. Planify a regular launch of the script
crontab -e
在檔案最下面加入(* 號不要去掉)
# automatically load loopback for new bluetooth devices (check every minute)
* * * * * /home/pi/pulseAudioBluetooth/checkForBluetoothDevice.sh  >> /home/pi/pulseAudioBluetooth/cron.log 2>&1
sudo reboot

以上如果正確無誤 IPAD及PI 在開機後連線後並撥放音樂即可在JACK 聽到輸出音樂

補助:一些藍芽指令
lsusb
hciconfig
hcitool dev
hcitool scan
hcitool inq
hcitool con
hcitool name 98:E7:9A:27:FA:64  
hcitool info 98:E7:9A:27:FA:64 
hciconfig hci0 piscan 打开PSCAN和ISCAN
hciconfig -a

沒有留言:

張貼留言