本文章主要记录下linux上移植bluez的一些要点流程,以rv1109设备为例说明,蓝牙芯片为rtl8723ds。
- 根据硬件连接,确定蓝牙串口协议是H4 还是 H5,这两个主要差别在于H4是三线串口(rx,tx,gnd),H5是五线串口(三线基础上另加CTS、RTS),确定好串口传输协议,后面就按对应配置选择。
- 根据厂商提供的资料,配置kernel支持蓝牙,并编译蓝牙驱动ko,这部分问题不大。
- 厂商提供的资料中寻找rtk_hciattach工具源码,编译。
- 编译bluez以及相关依赖,rv1109的sdk使用buildroot构建,里面已经有bluez相关工具包了,我们只需要make menuconfig配置下打开,注意还有些相关依赖库,配置完毕编译出新的镜像。
- 含有bluez的镜像烧写运行后,就可以把前面编出来的蓝牙驱动ko放进来了,然后插入ko,
insmod hci_uart.ko
。 - 使用rtk_hciattach初始化蓝牙(注意修改对应的串口节点)
- 把前面编译好的rtk_hciattach工具拷贝到板端系统/bin下面,并修改其权限
- 创建目录/lib/firmware/rtlbt,在供应商提供的资料中找到rtl8723d_config和rtl8723d_fw,拷贝到/lib/firmware/rtlbt,注意有无串口流控,对应的拷贝文件不一样,需要供应商提供
- rtk_hciattach -n -s 115200 ttyS0 rtk_h5 & //初始化蓝牙
- 启动dbus
- adduser messagebus
- 把第4步中生成的dbus-daemon和dbus-uuidgen拷贝到/bin下面,并修改其权限
- dbus-daemon --print-address --system 或者dbus-daemon --config-file=/usr/share/dbus-1/system.conf //运行dbus
- 上面的命令运行后可能会报错,有可能是cnof文件或者相应的依赖库缺少,根据上面报错的提示,在第4步中编出来的文件找到system.conf和bluetooth.conf等放到提示的目录下,设备上没有的目录则创建。缺失的lib库也拷贝到/lib下面
举例如下:/mnt/# dbus-daemon --print-address --system Failed to start message bus: Failed to open "/usr/local/etc/dbus-1/system.conf": No such file or directory(需要拷贝此文件) /mnt/# dbus-daemon --print-address --system Failed to start message bus: Failed to read directory "/usr/local/etc/dbus-1/system.d": No such file or directory(需要创建此目录/usr/local/etc/dbus-1/system.d/) /mnt/outdir# dbus-daemon --print-address --system Failed to start message bus: Failed to bind socket "/usr/local/var/run/dbus/system_bus_socket": No such file or directory(需要创建/usr/local/var/run/dbus/)
上述过程中若提示/mnt/# dbus-daemon --print-address --system Unknown group "lp" in message bus configuration file unix:path=/usr/local/var/run/dbus/system_bus_socket,guid=4c34eaaa4681006ae29c7791386d99d8 或者 ~ # dbus-daemon --config-file=/usr/local/etc/dbus-1/system.conf Unknown group "lp" in message bus configuration file
则删除/usr/local/var/run/dbus/pid,每次开机都需要删除这个/mnt/outdir # dbus-daemon --print-address --system Failed to start message bus: The pid file "/usr/local/var/run/dbus/pid" exists, if the message bus is not running, remove this file
- 把第4步中生成的bluetoothd,bluetoothctl(可选hciconfig,hcitool,l2ping)等拷贝到/bin下面,修改其权限
- 运行bluetoothd
- bluetoothd -d -n -E&
- 根据提示在第4步的生成物中查找依赖库,并拷贝到/lib下面
则把第4步中生成的bluetooth.conf拷贝到/usr/local/etc/dbus-1/和/usr/local/etc/dbus-1/system.d/目录下/mnt/outdir # bluetoothd -d -n -E& /mnt/outdir # bluetoothd[7028]: Bluetooth daemon 5.44 D-Bus setup failed: Connection ":1.0" is not allowed to own the service "org.bluez" due to security policies in the configuration file bluetoothd[7028]: Unable to get on D-Bus
- 蓝牙测试,两种方法(最好配合手机上的蓝牙测试助手等)
- 使用bluetoothctl测试
/mnt# bluetoothctl //运行bluetoothctl [bluetooth]# show //查看控制器的 Power 是否为 yes,如果 Power 为 no,则运行 power on [bluetooth]# power on [bluetooth]# agent NoInputNoOutput //可以设置其他 IO caps, 如 KeyboardDisplay [bluetooth]# default-agent [bluetooth]# advertise on //开启ble广播 [bluetooth]# discoverable on //允许被发现 [bluetooth]# scan on //扫描到对应的设备后,使用 scan off 关闭 scan。 [bluetooth]# pair 00:22:48:DC:89:0F //配对远端设备。 [bluetooth]# connect 00:22:48:DC:89:0F //连接远端设备 如果没有加入蓝牙音频相关协议,connect时可能会报错,使用手机上蓝牙测试助手进行无配对测试也可以
- 使用hciconfig以及hcitool测试
hciconfig hci0 up hciconfig hci0 piscan hciconfig hci0 noencrypt hciconfig hci0 noauth hciconfig hcitool scan //搜索 l2ping F8:A2:D6:D4:6C:04 //简单ping测试 l2test -O 675 -s A0:9F:10:7E:5C:E3 l2test -I 675 -b 10000 -r
- 使用bluetoothctl测试