树莓派python蓝牙_树莓派2model B 通过蓝牙实现A2DP协议连接手机播放音乐

使用树莓派,开启他的A2DP功能,实现用手机蓝牙控制树莓派播放歌曲。主要操作过程参考http://wangye.org/blog/archives/921/以及英文博客http://blog.sina.com.cn/s/blog_89f6a9130101fi07.html

本机机型:树莓派2modelB

蓝牙适配器4.0版本(iphone和android机均可以用)

2.0版本(iphone不能使用,android可以,苹果的就是傲娇)

开机进入图形界面

在开始提解决方法时,重要的是看会看log!本次我们需要关心的是:/var/log/syslog和/var/log/bluetoth_dev

主要在前面两个博文上根据自己的需求提出的改进:

配置分组。配置好后会出现各种bug,其中已一个

raspberrypi pulseaudio[2563]: [pulseaudio]

protocol-native.c: Denied access to client with invalid authorization data.

看到网上的解决方案说:

关于shairport使用声音服务Access

Denied的问题,你将运行shairport进程的用户加入pulse-access组就可以了。

所以我做的操作是:

sudo usermod -a -G

pulse-access pi

sudo usermod -a -G pulse-access root(我自己在使用过程中都是切换到root身份)

修改设备蓝牙的名字和属性

名字修改没有问题,主要是属性修改,在/etc/bluetooth/main.config中是不会出现问题的,但是在

/var/lib/bluetooth/<你的蓝牙MAC地址>/config下,修改后每次开机都会产生变化为

Class0x4e041c所以,这里需要我们再修改main.config文件下的class=0x4e041c。配置蓝牙设备接入步骤

并没有发现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"

播放没有声音

查看系统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"

替换后就有了!

运行update-rc.d

bluetooth-agent defaults报错,导致每次开机服务开不起来。。。

解决办法也是参考他人的

在debian6中使用update-rc.d会报错,如下:

update-rc.d: using

dependency based boot sequencing

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

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

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创建密码,将需要的文件拷到用户目录。再就是在目录下执行

chmod777 bluetooth.sh

./bluetooth.sh

最后重启一下!

#!/bin/bash

echo

"==============================================================================="

echo "使用华中科技大学的源"

sudo cp -f

~/bluetooth/sources.list /etc/apt/sources.list

echo "bluetooth

config!"

echo "install

apt"

sudo

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

sudo

apt-get upgrade -y #升级系统软件

echo

"=============================install

vim========================================"

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-2

bluez-tools

echo "配置安装的软件"

echo "============================配置分组规则======================================="

sudo usermod -a -G

lp pi

sudo usermod -a -G

pulse-access pi

sudo usermod -a -G

pulse-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 -d

bluetooth-agent

echo "===========================命令行以pi来登陆===================================="

sudo cp -f

~/bluetooth/inittab /etc/inittab

sudo cp -f

~/bluetooth/pulseaudio /etc/default/pulseaudio

echo "===========================开启蓝牙自发现======================================"

sudo hciconfig hci0

piscan

sudo

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值