BT[3]-BLE广播详解

一、HCI介绍

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实现的:

/* 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

# 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

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.
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

3、Bluetooth官网

4、蓝牙协议分析(5)_BLE广播通信相关的技术分析

5、一分钟读懂低功耗蓝牙连接数据包

6、BLE广播数据解析

原文地址:https://www.cnblogs.com/rockyching2009/p/10610288.html