SkylarkOS 蓝牙使用指南

优质
小牛编辑
120浏览
2023-12-01

编译配置说明

蓝牙服务在middleware层实现
开机后,后台自动运行bluetooth_service服务,该服务处理ble和经典蓝牙的功能,如何ble广告,收发数据(ble配网用),蓝牙播放歌曲A2DP等功能
代码在:/middleware/bluetooth

bluetooth_service依赖libbluetooth.default.so,真正的蓝牙底层操作,都在libbluetooth.default.so上实现的
代码在:/middleware/hal/modules/bluetooth

skylarkapp方案上使用

按照以下流程,就可以手机播放蓝牙音乐了

手动测试

middleware/bluetooth/service/docs下有对应的接口说明文档,指导第三方如何调用

middleware/bluetooth/service/demo目录下的bluetooth_test演示了如何使用bluetooth_service服务,对应的演示程序默认是编译在:

openwrt/build_dir/target-arm_cortex-a7+neon_glibc-2.22_eabi/bluetooth_service下

可以测试的项有:

蓝牙调试

bluez在openwrt中的编译

蓝牙使用bluez工具

编译:

make package/bluez/clean

make package/bluez/compile V=s


对应的工具,放在/usr/bin目录中,有:

bluemoon

bluetoothctl

bluetoothd

btmgmt

btmon


debug

bluez luez 官方有提供hcidump,可以使用 hcidump目录下stackhost 与 controller之间的 蓝牙数据交互流程

hcidump工具可以直接运行,将 loglog 打印在terminal,也可以用下方式将 logloglog保存在文件

保存在文件 :

hcidump –t>x.txt

hcidump –w x.txt

打造蓝牙播放器(A2DP Source)

第一步: 通过串口或adb登录板子终端.


第二步: 输入命令bluetoothctl,在bluetoothctl命令行下:
(注:bluetoothctl工具默认没有方到文件系统上,需要自己在编译的bluez目录下面,拷贝出来)

输入power on,激活蓝牙.

1.待蓝牙音箱打开后,输入scan on,开启扫描.

2.发现蓝牙音箱的设备MAC地址后,可输入scan off关闭扫描.

3.连接音箱,输入connect 11:58:02:B4:02:50(蓝牙音箱的MAC地址)

connect 2C:57:31:5E:4C:77

4.待连接成功后,bluetoothctl的命令行名字会变为对应的蓝牙音箱名字.


第三步: 配置pulseaudio a2dp sink,可再打开一个终端来操作

输入pactl list,找到对应的sink设备,比如:

Card #0

Name: bluez_card.11_58_02_B4_02_50

Driver: module-bluez5-device.c

Owner Module: 13

Properties:

    device.description = "HAVIT M8"

    device.string = "11:58:02:B4:02:50"

    device.api = "bluez"

    device.class = "sound"

    device.bus = "bluetooth"

    device.form_factor = "headset"

    bluez.path = "/org/bluez/hci0/dev_11_58_02_B4_02_50"

    bluez.class = "0x240404"

    bluez.alias = "HAVIT M8"

    device.icon_name = "audio-headset-bluetooth"

    device.intended_roles = "phone"

Profiles:

    headset_head_unit: Headset Head Unit (HSP/HFP) (sinks: 1, sources: 1, priority: 20, available: yes)

    a2dp_sink: High Fidelity Playback (A2DP Sink) (sinks: 1, sources: 0, priority: 10, available: yes)

    off: Off (sinks: 0, sources: 0, priority: 0, available: yes)

Active Profile: off

Ports:

    headset-output: Headset (priority: 0, latency offset: 0 usec)

        Part of profile(s): headset_head_unit, a2dp_sink

    headset-input: Headset (priority: 0, latency offset: 0 usec)

        Part of profile(s): headset_head_unit

如果看到“Active Profile: off” ,需激活a2dp profile:

pactl set-card-profile 0 a2dp_sink

0 表示 Card #0,表示的对应的声卡编号.

使用pactl info 查看系统输出的声卡设备.

Server String: /var/run/pulse/native
Library Protocol Version: 32
Server Protocol Version: 32
Is Local: yes
Client Index: 13
Tile Size: 4056
User Name: pulse
Host Name: OpenWrt
Server Name: pulseaudio
Server Version: 10.0
Default Sample Specification: s16le 2ch 44100Hz
Default Channel Map: front-left,front-right
Default Sink: bluez_sink.11_58_02_B4_02_50.a2dp_sink
Default Source: bluez_sink.11_58_02_B4_02_50.a2dp_sink.monitor
Cookie: 372e:4f33

如果Default Sink不是蓝牙声卡 “bluez_sink.11_58_02_B4_02_50.a2dp_sink”,需切换Default Sink:

pactl set-default-sink bluez_sink.11_58_02_B4_02_50.a2dp_sink

11_58_02_B4_02_50 是蓝牙音箱的MAC地址.


第四步: 使用智能音箱自带的播放器播放相应的歌曲.

可以使用paplay做测试验证