1. 程式人生 > >樹莓派2model B 通過藍芽實現A2DP協議連線手機播放音樂

樹莓派2model B 通過藍芽實現A2DP協議連線手機播放音樂

使用樹莓派,開啟他的A2DP功能,實現用手機藍芽控制樹莓派播放歌曲。主要操作過程參考http://wangye.org/blog/archives/921/以及英文部落格http://www.instructables.com/id/Turn-your-Raspberry-Pi-into-a-Portable-Bluetooth-A/

本機機型:樹莓派2model B

藍芽介面卡 4.0版本(iphoneandroid機均可以用)

 2.0版本iphone不能使用,android可以,蘋果的就是傲嬌)

開機進入圖形介面

在開始提解決方法時,重要的是看會看log!本次我們需要關心的是:/var/log/syslog/var/log/bluetoth_dev

主要在前面兩個博文上根據自己的需求提出的改進:

  1. 配置分組。配置好後會出現各種bug,其中已一個

raspberrypi pulseaudio[2563]: [pulseaudio]protocol-native.c: Denied access to client with invalid authorization data.

看到網上的解決方案說:

關於shairport使用聲音服務AccessDenied的問題,你將執行shairport程序的使用者加入pulse-access組就可以了。

所以我做的操作是:

sudo usermod -a -Gpulse-access pi

sudo usermod -a -G pulse-access root

(我自己在使用過程中都是切換到root身份)

  1. 修改裝置藍芽的名字和屬性

名字修改沒有問題,主要是屬性修改,在/etc/bluetooth/main.config中是不會出現問題的,但是在

/var/lib/bluetooth/<你的藍芽MAC地址>/config下,修改後每次開機都會產生變化為

Class0x4e041c所以,這裡需要我們再修改main.config檔案下的class=0x4e041c

  1. 配置藍芽裝置接入步驟

並沒有發現99-input.rules檔案,於是就在相應的路徑新建了一個檔案

vim /etc/udev/rules.d/99-input.rules

內容為:

SUBSYSTEM=="input",GROUP="input", MODE="0660"

KERNEL=="input[0-9]*",RUN+="/usr/lib/udev/bluetooth"

  1. 播放沒有聲音

檢視系統log,說的是大概是無法開啟/usr/lib/udev/bluetooth裡面的sink,也是看到網上的解決方法

2° output audio doesn't work

in this file: /usr/lib/udev/bluetooth :

Replace this line:

AUDIOSINK="alsa_output.platform-bcm2835_AUD0.0.analog-stereo"

With this:

AUDIOSINK="alsa_output.0.analog-stereo"

替換後就有了!

  1. 執行update-rc.d bluetooth-agent defaults報錯,導致每次開機服務開不起來。。。

解決辦法也是參考他人的

debian6中使用update-rc.d會報錯,如下:

update-rc.d: usingdependency based boot sequencing

可以使用insserv命令來代替update-rc.d

所以使用insserv -d bluetooth-agent代替原句!

  1. pulseaudio報錯

現在記不清具體是什麼錯誤了,我做的處理是

[warn] PulseAudio configured for per-user sessions ... (warning). 

Debian變種版本也會在啟動過程中彈出類似的警告資訊。為了解決這個問題,鍵入下面這個命令:leafpad /etc/default/pulseaudio 

找到這一行:PULSEAUDIO_SYSTEM_START=0 

把0換成1:

PULSEAUDIO_SYSTEM_START=1 

其中,0表示系統模式下不啟動PulseAudio,1表示系統模式下啟動PulseAudio。

其他的都是參照那兩篇博文做的操作。

出了問題一定記得看log

另外搜不到搜不到藍芽的時候嘗試操作sudo hciconfig hci0 piscan

搜到藍芽連線不上,嘗試sudo start-stop-daemon -S -x/usr/bin/bluetooth-agent -c pi -b -- 0000

再不行就重啟試試吧。

最後附上我寫的一個簡單的指令碼操作,再裝完系統後,為root建立密碼,將需要的檔案拷到使用者目錄。再就是在目錄下執行

chmod 777 bluetooth.sh

./bluetooth.sh

最後重啟一下!

#!/bin/bash

echo"==============================================================================="

echo "使用華中科技大學的源"

sudo cp -f~/bluetooth/sources.list /etc/apt/sources.list

echo "bluetoothconfig!"

echo "installapt"

sudoapt-get update -y #更新源列表

sudoapt-get upgrade -y #升級系統軟體

echo"=============================installvim========================================"

sudo apt-get install-y vim

sudo cp -f~/bluetooth/.vimrc /root/.vimrc

sudo apt-get install-y bluetooth bluez pulseaudio-module-bluetooth python-gobject python-gobject-2bluez-tools

echo "配置安裝的軟體"

echo "============================配置分組規則======================================="

sudo usermod -a -Glp pi

sudo usermod -a -Gpulse-access pi

sudo usermod -a -Gpulse-access root

echo "=========================藍芽啟用A2DP功能======================================"

sudo cp -f~/bluetooth/audio.conf /etc/bluetooth/audio.conf

sudo cp -f~/bluetooth/daemon.conf /etc/pulse/daemon.conf

echo "============================修改名字==========================================="

sudo cp -f~/bluetooth/main.conf /etc/bluetooth/main.conf

echo "修改裝置裡面的名字的方法還有確定這裡預留待"

bluetoothMac=$(ls/var/lib/bluetooth/)

sudo cp -f~/bluetooth/config /var/lib/bluetooth/*/config

echo "=============================配置藍芽接入======================================"

sudo cp -f~/bluetooth/99-input.rules /etc/udev/rules.d/99-input.rules

sudo mkdir/usr/lib/udev

sudo cp -f~/bluetooth/bluetooth /usr/lib/udev/bluetooth

sudo chmod 774/usr/lib/udev/bluetooth

echo "===========================開啟藍芽自動發現功能================================"

sudo cp -f~/bluetooth/bluetooth-agent /etc/init.d/bluetooth-agent

sudo chmod 755/etc/init.d/bluetooth-agent

sudo insserv -dbluetooth-agent

echo "===========================命令列以pi來登陸===================================="

sudo cp -f~/bluetooth/inittab /etc/inittab

sudo cp -f~/bluetooth/pulseaudio /etc/default/pulseaudio 

echo "===========================開啟藍芽自發現======================================"

sudo hciconfig hci0piscan

sudostart-stop-daemon -S -x /usr/bin/bluetooth-agent -c pi -b -- 0000

所有配置檔案下載連線<http://download.csdn.net/detail/zhaoqi_0612/9016779>