一、HCI介绍
1 2 | HCI: Host Controller Interface,it provides a uniform interface method of accessing a Bluetooth Controller’s capabilities. From [Vol 2 ] Part E: Host Controller Interface Functional Specification |
通过HCI提供的接口,只要按照蓝牙协议栈定义的数据格式发送指令/数据,蓝牙设备就会执行相应的动作。
HCI在蓝牙系统中的层次:
在Linux中,内核基于HCI Driver封装出了AF_BLUETOOTH类型的socket,用户空间通过socket接口就可以和peer之间收发数据。之前提到的hciconfig/hcitool工具就是基于socket实现的:
1 2 3 4 5 | /* Open HCI socket */ if ((ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) { perror ( "Can't open HCI socket." ); exit (1); } |
更多细节查看相应命令的源码。
二、广播命令解析
注:HCI命令格式定义详见:5.4 Exchange of HCI-Specific Information
1 2 3 4 5 | # hcitool -i hci0 cmd 0x08 0x000a 01 < HCI Command: ogf 0x08, ocf 0x000a, plen 1 01 > HCI Event: 0x0e plen 4 01 0A 20 00 |
0x08:For the LE Controller Commands, the OGF code is defined as 0x08.
0x000A:HCI_LE_Set_Advertise_Enable
01:0x000A命令的参数,1为enable,0为disable广播
HCI Event:HCI命令交互过程是Request-Response,所以这里就是命令的响应
再比如:
# hcitool -i hci0 cmd 0x08 0x0008 16 02 01 06 03 02 80 ff 0e 09 62 6c 65 5f 6e 61 6d 65 5f 5a 30 30 31
LE设置广播数据 数据长度
广播数据包,2个字节,内容01 06
广播数据包,3个字节,内容02 80 ff
广播数据包,14个字节,设备简称“ble_name_Z001”
广播数据包格式:CS [Vol 3] Part C, Section 11
三、GATT
1 2 | GATT:Generic Attribute Profile GATT is designed to be used by an application or another profile, so that a client can communicate with a server.<br>From [Vol 3] Part G:Generic Attribute Profile(GATT), also refer Part F: Attribute Protocol(ATT) |
GATT是低功耗蓝牙属性应用规范,用于主机和从设备之间的数据传输。
bluez-5.50/tools/btgatt-server.c和bluez-5.50/tools/btgatt-client.c提供了使用GATT传输数据的范例,之前的心率计就是这里实现的,是研究的好资料。
参考
1、Bluetooth Core Specification Version 4.1
2、BlueZ
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!