天天看点

【蓝牙】基于Bluez的Bluetoothctl BLE开发

对于BLE的开发,大部分的BLE开发者都是在BLE芯片原厂的SDK基础上进行BLE的应用开发,跟更多的是基于SDK的应用开发而对于BLE的协议、框架知之甚少. 另外芯片原厂也不会暴露过多底层的接口给到开发者,开发者想要深入理解也无法下手,然后Linux平台提供了一个开源的蓝牙协议栈(传统蓝牙+BLE), 给了BLE开发者一个深入理解的入口,无论你是Linux还是RTOS的开发人员,只要希望能够对BLE有更深入的理解,都希望能开始接触到Linux下的BLE Over Bluez的开发、学习. 本篇文章主要是基于Bluez提供的bluetoothctl工具,完成基于Bluez的BLE广播包注册、BLE服务添加、BLE服务下的特征添加, 带大家先从工具的方式开始上手BLE Over Bluez的开发.

开发环境准备

工具和软件准备

  1. BLE Dongle 我这里使用的CSR4.0
  2. Ubuntu 20.04 我这里是Windows 10下VM安装的Ubuntu 20.04虚拟机
  3. 再安装并启动Ubuntu后,打开终端并确认系统是否有bluetoothctl命令,没有的话请自行安装 Bluez 5.58. 正常情况下是默认安装

环境确认

  1. 将BLE Dongle插入USB口,并将识别到BLE Dongle链接到虚拟机(如果使用的虚拟机的方式),如下图需要选择连接到虚拟机
    【蓝牙】基于Bluez的Bluetoothctl BLE开发
  2. 在Ubuntu 虚拟机识别到该BLE Dongle后,会在VM的右下角识别到一个BLE设备,界面如下截图,
    【蓝牙】基于Bluez的Bluetoothctl BLE开发
  3. 确认Bluez的Bluetoothd服务是否起来,在终端运行服务查看命令, 确认是否有bluetooth服务
➜  /tmp service --status-all 
 [ + ]  acpid
 [ - ]  alsa-utils
 [ - ]  anacron
 [ + ]  apparmor
 [ + ]  apport
 [ + ]  avahi-daemon
 [ + ]  bluetooth
 [ - ]  console-setup.sh
 [ + ]  cron
 [ + ]  cups

           
  1. 在确认Bluetooth服务器起来后,在终端运行bluetoothctl 命令, 然后执行show命令查看蓝牙相关信息.

    UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb) 就是我们所熟悉的GATT

➜  /tmp bluetoothctl 
[NEW] Controller 00:1A:7D:DA:71:13 Sugr #2 [default]
[NEW] Controller BC:83:85:05:80:91 Sugr 
[NEW] Device 00:00:00:0F:44:32 Listening Bar
[NEW] Device 00:09:A7:4D:99:1D DH3-v2.2.3
Agent registered
[bluetooth]# show
Controller 00:1A:7D:DA:71:13 (public)
	Name: Sugr #2
	Alias: Sugr #2
	Class: 0x000c0000
	Powered: yes
	Discoverable: no
	Pairable: yes
	UUID: Headset AG                (00001112-0000-1000-8000-00805f9b34fb)
	UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
	UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
	UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
	UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
	UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
	UUID: Audio Source              (0000110a-0000-1000-8000-00805f9b34fb)
	UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
	UUID: Headset                   (00001108-0000-1000-8000-00805f9b34fb)
	Modalias: usb:v1D6Bp0246d0530
	Discovering: no

           
  1. 然后再运行bluetoothctl操作界面下的advertise on 指令,尝试打开BLE广播包,来确认Bluez的BLE模块是否初步正常. 显示如下表示正常.
[bluetooth]# advertise on
[CHG] Controller 00:1A:7D:DA:71:13 SupportedInstances: 0x04
[CHG] Controller 00:1A:7D:DA:71:13 ActiveInstances: 0x01
Advertising object registered

           

如何通过bluetoothctl开启广播包并注册相关BLE服务

设置并创建打开BLE广播

在终端运行bluetoothctl后,通过运行执行menu advertise命令,进入advertise的子命令操作窗口.

[bluetooth]# menu advertise 
Menu advertise:
Available commands:
-------------------
set-uuids [uuid1 uuid2 ...]                       Set advertise uuids
set-service [uuid] [data=xx xx ...]               Set advertise service data
set-manufacturer [id] [data=xx xx ...]            Set advertise manufacturer data
set-tx-power <on/off>                             Enable/disable TX power to be advertised
set-name <on/off/name>                            Enable/disable local name to be advertised
set-appearance <value>                            Set custom appearance to be advertised
set-duration <seconds>                            Set advertise duration
set-timeout <seconds>                             Set advertise timeout
back                                              Return to main menu
version                                           Display version
quit                                              Quit program
exit                                              Quit program
help                                              Display help about this program

           

然后执行set-manufacturer 和 set-name 来设置广播包中携带的设备制造商信息和设备名

下面示例,只是个简单演示

[bluetooth]#  manufacturer 0xffff 0x12 0x34
[bluetooth]#  name Tracy

           

最后运行back命令退回到上一窗口执行开广播的命令: advertise on

[bluetooth]# advertise on
[CHG] Controller 00:1A:7D:DA:71:13 SupportedInstances: 0x04
[CHG] Controller 00:1A:7D:DA:71:13 ActiveInstances: 0x01
Advertising object registered

           

补充说明:

如果出现如下界面,说明之前已经注册过需运行 advertise off后再执行advertise on

[bluetooth]# advertise on
Advertisement is already registered

           

在执行完以上步骤后就可以打开手机上的BLE测试App进行广播包的查看确认了. 我这里使用的是Nordic的nRF Connect.

你会看到在App的SCANNER界面会出现一台叫Tracy(我在本例子中set-name使用的字段是Tracy)的BLE设备. 并确认广播包中Manufacturer data和前面是一致的.

【蓝牙】基于Bluez的Bluetoothctl BLE开发
【蓝牙】基于Bluez的Bluetoothctl BLE开发

注册BLE Service

在终端运行bluetoothctl后,通过运行执行menu gatt命令,进入gatt的子命令操作窗口.

[bluetooth]# menu gatt
Menu gatt:
Available commands:
....
           
  1. 执行register-service添加自定义的BLE Service
[bluetooth]# register-service e2d36f99-8909-4136-9a49-d825508b297b
[NEW] Primary Service
	/org/bluez/app/service0x55b6748c3010
	e2d36f99-8909-4136-9a49-d825508b297b
	Vendor specific
[/org/bluez/app/service0x55b6748c3010] Primary (yes/no): yes

           
  1. 执行register-characteristic创建一个read属性的characteristic.
[bluetooth]# register-characteristic 0x1234 read
[NEW] Characteristic
	/org/bluez/app/service0x55cec6ed2110/chrc0x55cec6ee2360
	0x1234
	Unknown
[/org/bluez/app/service0x55cec6ed2110/chrc0x55cec6ee2360] Enter value: 1

           
  1. 执行register-characteristic创建一个write/read属性的characteristic.
[NEW] Characteristic
	/org/bluez/app/service0x55cec6ed2110/chrc0x55cec6ee2770
	0x4567
	Unknown
[/org/bluez/app/service0x55cec6ed2110/chrc0x55cec6ee2770] Enter value: 1
           
  1. 然后执行 register-application
  2. 最后和前面类似,打开Nodirc的nRF Connect,找到BLE设备"Tracy",点击连接,会发现多了自定义的BLE Serice(0x1800/0x1801 之外)

补充说明,千万记得再重新连接之前先清除App的缓存数据,否则即使连接上了也无法发现后添加的自定义服务,这是因为nRF Connect并没有重新进行服务的发现并更新

总结

以上内容主要是简单讲述了如何基于Bluez的bluetootctl进行BLE相关的操作,后续会有相关的博客对BLE的开发做更深入的分享.

参考链接:

Creating a BLE Peripheral with BlueZ

Bluez官网

继续阅读