Widora-NEO: 连接蓝牙音箱

试验了一下用Widora-NEO来连接蓝牙小音箱,发现配置过程有点复杂,记录下步骤供参考:
0) 在make menuconfig的时候勾选dbus和bluez包。

  1. 编译支持A2DP profile的Pulseaudio,
    1.1) 前提是需要有SBC库,这个可以在make menuconfig的时候勾选,sbc是用来对蓝牙音频数据进行压缩的。
    1.2) 对pulseaudio 8.0 的Makefile稍微进行了一下修改, DEPENDS增加了sbc
...   
define Package/pulseaudio/Default
 SECTION:=sound
 CATEGORY:=Sound
 DEPENDS:=+sbc +libspeexdsp +libsndfile +libltdl +libpthread +libdbus \
   +libbluetooth +librt +alsa-lib +libopenssl +libwrap +libcap $(ICONV_DEPENDS) $(INTL_DEPENDS)
 TITLE:=Network sound server
 MAINTAINER:=Peter Wagner <tripolar@gmx.at>
 URL:=http://www.pulseaudio.org
 PROVIDES:=pulseaudio
 USERID:=pulse=51:pulse=51
endef
...

修改CONFIGE配置参数, 主要是不要–disable-bluez 和 --disable-dbus 项,必须让它支持bluez和dbus.

...
CONFIGURE_ARGS += \
	--with-system-user=pulse \
	--with-system-group=pulse \
	--with-access-group=audio \
	--with-database=simple \
	--enable-alsa \
	--disable-hal \
	--disable-gconf \
	--disable-nls \
	--disable-manpages \
	--enable-oss-output \
	--disable-oss-wrapper \
	--disable-samplerate \
	--disable-per-user-esound-socket \
	--disable-gtk3 \
	--disable-glib2 \
	--disable-jack \
	--disable-asyncns \
	--disable-lirc \
	--disable-udev \
	--without-fftw \
	--disable-avahi \
...

1.3) 附上修改后的Makefile 文件1523779902364-makefile (4.4 KB)
用它替换 widora_openwrt/feeds/packages/sound/pulseaudio目录下的Makefile.
1.4) make menuconfig选中sbc, bluez,pulse,dbus相关的包:
1.4.1) Libraries: sbc
1.4.2) Sound: pulseaudio-daemon, pulseaudio-profiles, pulseaudio-tools
1.4.3) Utilities: bluez-daemon, bluez-examples,bluez-utils, dbus, dbus-utils
1.4.4) Kernel modules -> Input modules: kmod-input-uinput
1.5) 然后进行固件编译。 完成的库文件里应该包含 module-bluez5-device.so, module-bluez5-discover.so, module-bluetooth-discover.so 和 module-bluetooth-policy.so, 这样就可以了。

  1. /etc/bluetooth目录下的配置文件
    1523778718604-bluetooth.tar.gz (2.36 KB)

  1. /etc/pulse 目录下的配置文件
    请参见本论坛的资料:将Widora-NEO作为pulseaudio网络声音播放端
    1523778735069-pulse.tar.gz (3.06 KB)

  1. /etc/dbus-1 目录下的配置文件
    1523778748557-dbus-1.tar.gz (3.27 KB)

  1. 操作bluetoothctl进行蓝牙设备的配对和连接
    先前将bluetoothd 和 pulseaudio的开机启动项取消,不让它们在system session下运行:
    /etc/init.d/bluetoothd disable
    /etc/init.d/pulseaudio disable
    开机确认dbus已经随系统启动了。
    确认蓝牙适配器已插入。
    5.1 启动pulseaudio daemon: pulseaudio -D --exit-idle-time=-1
    5.2 启动bluetoothd到后台: bluetoothd -E -C &
    ( /etc/pulse/default.pa中沒有加載module-alsa-sink的話,此時可以加載:
    load-module module-alsa-sink device=xxx )
    5.3 启动bluetoothctl: bluetoothctl
    5.3.1 power on
    5.3.2 agent on
    5.3.3 default-agent
    5.3.4 scan on (等搜索到你的蓝牙设备)
    5.3.5 trust xx:xx:xx:xx:xx:xx
    5.3.6 pair xx:xx:xx:xx:xx:xx (等完成配对)
    5.3.7 connect xx:xx:xx:xx:xx:xx (等完成连接)
    5.3.8 quit (连接成功后退出bluetoothctl)

  1. 设置A2DP sink
    6.1 执行命令: pacmd ls
    最后可以看到蓝牙设备支持的各种服务(profiles)
    !
    6.2 执行命令: pacmd set-card-profile 0 a2dp_sink ( 0 是 card index)
    选择A2DP服务, 完成后用命令 pacmd list-sinks 可以看到A2DP已经加入到sinks中 :( !!注意:這個sink_index不是固定不變的,每次要看一下。不然後面的播放命令 paplay -d sink_index 會出錯。)
    !

  1. 通过蓝牙音箱播放音乐
    7.1 播放一段音乐:
    paplay -d 2 xxx.wav ( -d 2 对应pulse的sink index, 见第6步 )
    7.2 用 pacmd set-sink-volume 2 0xb000 来调节音量。 最好不要大于0xffff, 太大会有破音。
    7.3 可以調整時延:
    pacmd --latency-msec=50 -d 2 xxx.wav

*** 其實也可以用pulse默認設定, 這樣就不需要第6步驟了。 paplay的時候也不用指定sink了。


  1. 由于Widora Openwrt的ALSA还不支持PULSE,所以使用mplayer 的时候可以通过管道将音频转成wav文件后给paplay播放:
    !

  1. 如果想把默认采样率设置为48k, 那么需要在ALSA配置文件和 /etc/pulse/daemon.conf 中都设置成48k.

  2. 反過來,從手機連接到Widora也是同樣的,要注意的是讓 load-module module-alsa-sink device=dmix 在啓動bluetoothd和pulseaudio後再加載,這樣可以避免resample產生雜音。

可以使用了:http://v.youku.com/v_show/id_XMzU0MDI2OTQ3Mg==.html?spm=a2hzp.8253869.0.0


问题:

  1. 有时会出来如下错误提示,等它过去. 出现这个错误,一般是没有进行 第6步 设置A2DP sink 造成的:
[ 1158.650000] Bluetooth: hci0 SCO packet for unknown connection handle 0      
[ 1158.660000] Bluetooth: hci0 SCO packet for unknown connection handle 65535      
[ 1158.660000] Bluetooth: hci0 SCO packet for unknown connection handle 256
[ 1158.670000] Bluetooth: hci0 SCO packet for unknown connection handle 0      
[ 1158.680000] Bluetooth: hci0 SCO packet for unknown connection handle 1

!

  1. 斷開一個連接後,需要重新killall -9 pulseaudio 和 bluetoothd,然后重起pulseaudio和bluetoothctl流程。不然第2次的連接無效,pacmd ls命令會僵死。 在斷開現有連接前先 pulseaudio -k && pulseaudio -D --exit-idle-time=-1 在通過bluetoothctl重新連接就可以。
  2. 把Widora-NEO當成手機的藍牙播放器來使用時,播放時按下暫停鍵的時間太長的話, pulseaudio會僵死,需要重新啓動。ALSA 接口問題?
  3. HSP(手机音频) , A2DP(高级音频传送), HSP 只支持 8K的采样率,而 A2DP的为44.1k和48k.
  4. USB藍牙適配器好像只能掛在usb root hub下才能工作。

@midas-zhou 厉害!