树莓派适配蓝牙,将声音输出到蓝牙音响

插入蓝牙。

###连接蓝牙设备

add nao to group plugdev
> usermod -a -G plugdev nao

sudo hciconfig hci0 up 这一步会报错:
Can’t init device hci0: Operation not possible due to RF-kill (132)

然后可以通过 rfkill list 看soft block状态:

0: hci0: Bluetooth
Soft blocked: yes
Hard blocked: no
1: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no

可以看到 Bluetooth是处于 soft blocked 的状态,可是通过 rfkill unblock all 或 rfkill unblock bluetooth 都没效果。

查资料可能是由于内核模块 acer-wmi 冲突造成的,解决办法:
sudo modprobe -r acer-wmi
cd /etc/modprobe.d
sudo nano blacklist.conf
来自:http://askubuntu.com/questions/62730/cannot-change-soft-block-setting-to-no-for-ar9287-wireless-network

然后naoqi系统里面不存在 acer-wmi模块。

后来发现是 connman 造成的,解决办法:
root@osmc:~$ cat /var/lib/connman/settings
[global]
OfflineMode=false

[Wired]
Enable=true
Tethering=false

[Bluetooth]
Enable=false
Tethering=false

Setting Enabled=true under the Bluetooth tag fixed the soft block。

或直接用命令 connman enable bluetooth
来自:https://discourse.osmc.tv/t/enabling-bluetooth/711

修改后需要重启系统。

之后再 ,hciconfig hci0 up

其中 hci0 是通过 hciconfig -a 拿到的。

再通过命令扫描周围的蓝牙:
hcitool scan
扫描结果可能是:
Scanning …
CLLSPKPCARG

找到目标的 hadware_id后再运行:
simple-agent hci0 或有的命令为 bluez-simple-agent

再运行
hcitool cc && hcitool auth

查看已经认证过的蓝牙列表:
bluez-test-device list

编辑配置文件 /etc/asound.conf ,加入

pcm.bluetooth {
type bluetooth
device
}

再编辑 vi /etc/bluetooth/audio.conf,加入:
Disable=Media
Enable=Socket

重启 sudo /etc/init.d/bluetooth restart

然后测试一下:

mpg321 -a bluetooth -g 15 02 Magic.mp3
aplay -D bluetooth bluetooth-music.wav

###修改系统的默认输出设备

使用 pactl list 出现以下报错:
Connection failure: Connection refused
pa_context_connect() failed: Connection refused
或使用pacmd 以下错:
No PulseAudio daemon running, or not running as session daemon.

解决办法: pulseaudio –kill && pulseaudio –start

如果 pactl list sinks short 命令没有出现蓝牙设备的sink,那么运行:pactl load-module module-alsa-sink device=bluetooth
参考:http://askubuntu.com/questions/145733/a2dp-connection-not-shown-in-pulseaudio

当蓝牙断开连接时,需要unload-module,参考 https://gist.github.com/joergschiller/1673341

再设置 pacmd set-default-sink 1
参考如何 动态切换audio输出 sink :
http://askubuntu.com/questions/71863/how-to-change-pulseaudio-sink-with-pacmd-set-default-sink-during-playback
http://unix.stackexchange.com/questions/65246/change-pulseaudio-input-output-from-shell

设置后,naoqi系统需要重启才能生效。

参考 :http://blog.whatgeek.com.pt/2014/04/raspberry-pi-bluetooth-wireless-speaker/
NAO设置参考:https://community.aldebaran.com/en/forum/problem-about-bluetooth-earphone-configuration-nao-3875

参考NAO对接蓝牙视频:https://www.youtube.com/watch?v=57Z4vLBgynI

2 – Create a serial port over BlueTooth:
> bluez-test-serial 00:07:80:51:10:73
Connected /dev/rfcomm0 to 00:07:80:51:10:73
Press CTRL-C to disconnect

Note the serial port name: “/dev/rfcomm0”

13 – Open, read and write using that port, eg using python and pyserial.
We’ve made an example using choregraphe: the simple_serial.py
(the serial port can be changed clicking the small tools at the bottom left of the choregraphe box)