arm 使用bluetoothctl连接蓝牙耳机

环境:ubuntu 14.04

目标:arm板

使用IMX6的arm板,接了一个USB外接蓝牙,使用的是bluz 5.50版本协议栈,使用USB蓝牙连接蓝牙耳机,最终要能通过蓝牙耳机录音。

安装 bluez,这个软件包提供蓝牙的协议栈

安装 bluez-utils, 其提供 bluetoothctl 工具

 

插入USB蓝牙后之后,可以使用lsusb命令查看USB设备

运行hciconfig可以看到USB蓝牙

第一步,先确保pulseaudio已经启动

# 查看pulseaudio是否在运行
pgrep -af pulseaudio
# 或者
ps -A |grep pulseaudio
# 如果没运行以前要先启动pulseaudio,如果连接蓝牙耳机时,就会直接返回连接失败
# 参数说明:--start   Start the daemon if it is not running
# pulseaudio -h    可以查看帮助
/usr/bin/pulseaudio --start


# 
pulseaudio --kill

PulseAudio 5.x 开始默认支持 A2DP。 确保这些包已经安装Install: pulseaudio-alsa, pulseaudio-bluetooth, bluez, bluez-libs, bluez-utils, bluez-firmwareAUR. 如果没有安装 pulseaudio-bluetooth,蓝牙设备在配对完成后,连接会失败,而且你不会得到任何有用的提示。

 

第二步,启动bluetoothd服务

# 我使用的arm板是通过如下命令启动bluetoothd
/etc/init.d/bluetooth start

/etc/init.d/bluetooth文件内容如下:

#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DESC=bluetooth

DAEMON=/usr/libexec/bluetooth/bluetoothd

# If you want to be ignore error of "org.freedesktop.hostname1",
# please enable NOPLUGIN_OPTION.
# NOPLUGIN_OPTION="--noplugin=hostname"
NOPLUGIN_OPTION=""
SSD_OPTIONS="--oknodo --quiet --exec $DAEMON -- $NOPLUGIN_OPTION"

test -f $DAEMON || exit 0

# FIXME: any of the sourced files may fail if/with syntax errors
test -f /etc/default/bluetooth && . /etc/default/bluetooth
test -f /etc/default/rcS && . /etc/default/rcS

set -e

case $1 in
  start)
	echo "Starting $DESC"

	if test "$BLUETOOTH_ENABLED" = 0; then
		echo "disabled. see /etc/default/bluetooth"
		exit 0
	fi

	start-stop-daemon --start --background $SSD_OPTIONS
	echo "${DAEMON##*/}"

  ;;
  stop)
	echo "Stopping $DESC"
	if test "$BLUETOOTH_ENABLED" = 0; then
		echo "disabled."
		exit 0
	fi
	start-stop-daemon --stop $SSD_OPTIONS
	echo "${DAEMON}"
  ;;
  restart|force-reload)
	$0 stop
	sleep 1
	$0 start
  ;;
  status)
	 pidof ${DAEMON} >/dev/null
	 status=$?
        if [ $status -eq 0 ]; then
                 echo "bluetooth is running."
        else
                echo "bluetooth is not running"
        fi
        exit $status
   ;;
   *)
	N=/etc/init.d/bluetooth
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0

# vim:noet

第三步,使能USB蓝牙设备

hciconfig hci0 up

 可以试一下扫描蓝牙设备

# 可以使用如下命令扫描当前可见的蓝牙,不过似乎看到有人说这个命令有些过时,但是我觉得挺好用的
hcitool scan
# 使用l2ping可以像ping命令一样检查蓝牙是否能在线
# 蓝牙地址比如:E3:28:E9:24:21:07
l2ping 蓝牙地址
# sdptool browse查看蓝牙可用服务
sdptool browse E3:28:E9:24:21:07

第四步,使用bluetoothctl连接蓝牙耳机

# 进入bluetoothctl命令行
bluetoothctl
# 进入bluetoothctl命令行后,类似:[bluetooth]#
# Controller代表arm板上的蓝牙设备,Device代表蓝牙耳机
# 按如下步骤初始化蓝牙设备,并连接蓝牙耳机
# 查看帮组
help
# 
power on
agent on
default-agent 
# 启动扫描
scan on 
# 假如蓝牙耳机地址为:E3:28:E9:24:21:07
trust E3:28:E9:24:21:07
# 配对
pair E3:28:E9:24:21:07
# 连接
connect E3:28:E9:24:21:07


# 查看蓝牙耳机信息
info E3:28:E9:24:21:07



# 断开连接
disconnect E3:28:E9:24:21:07
# 不想自动连接上蓝牙耳机,可以删除配对信息
remove E3:28:E9:24:21:07

不出什么问题,则已经连接上蓝牙耳机了。

如果发现连接上了但是蓝牙耳机的音频用不了要做如下检查。

# 查看当前音频卡,是否有蓝牙的耳机,蓝牙耳机对应设备一般以蓝牙地址作为名称的一部分,
# 比如:name: <bluez_card.E3_28_E9_24_21_07>
pacmd list-cards
# 查看音频输入源,是否有蓝牙的耳机
pacmd list-sources
# 比如:name: <bluez_source.E3_28_E9_24_21_07>
# 如果发现有name: <bluez_sink.E3_28_E9_24_21_07.monitor>
# 但是没有bluez_source.E3_28_E9_24_21_07
# 说明蓝牙配置不对
# bluez_sink.E3_28_E9_24_21_07.monitor是不能用于录音的

蓝牙耳机设置设置如下类型:

a2dp_sink          -- High Fidelity Playback (A2DP Sink) (sinks: 1, sources: 0, priority: 10, available: yes)
headset_head_unit  -- Headset Head Unit (HSP/HFP) (sinks: 1, sources: 1, priority: 20, available: yes)
off                -- Off (sinks: 0, sources: 0, priority: 0, available: yes)

这里可选择"a2dp_sink"或"headset_head_unit"两种配置,其中"headset_head_unit"可以支持音频输入/输出,"a2dp_sink"只支持输出。

所以设置为headset_head_unit才能有蓝牙输入

# 修改配置为:headset_head_unit
pacmd set-card-profile bluez_card.E3_28_E9_24_21_07 headset_head_unit
# 查看可用于播放的音频设备
pacmd list-sinks
# 查看帮助
pacmd help
# 查看状态,包括默认输入输入音频,默认采样等
pacmd stat


# 设置默认音频输出设备
pacmd set-default-sink bluez_sink.E3_28_E9_24_21_07
# 设置默认音频输入设备,默认麦克风
pacmd set-default-source bluez_source.E3_28_E9_24_21_07

 

参考:

https://wiki.archlinux.org/index.php/Bluetooth_headset_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)

https://www.cnblogs.com/zjutlitao/p/9576589.html

https://wiki.archlinux.org/index.php?title=PulseAudio_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&oldid=349911

http://blog.lujun9972.win/blog/2017/07/18/%E5%9C%A8archlinux%E4%B8%AD%E4%BD%BF%E7%94%A8%E8%93%9D%E7%89%99%E8%80%B3%E6%9C%BA/

https://blog.csdn.net/chenjk10/article/details/89283578

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章