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

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