蓝牙协议栈(Bluetooth stack)简介

1 前言

 本文只要对蓝牙协议栈做一个简单介绍,包含hci层、acl链路、

l2cap层,sdp服务及几个常见的profile,让初学蓝牙协议栈的人
对整个蓝牙协议栈有一个整体框架的了解。

2 HCI层

2.1 Hci命令及应答的控制

由于不同的hci命令对结果有不同的处理,有的不需要结果,有些需要

等待结果后才能执行下一步动作,有些在一定状态下才有效,对这
样在hci层,都有一套机制,每发送一个命令,都会先检查当前是否
可以发送该命令(有时hci controller当前不能接收命令时,也不能
发送),若命令可以发送,检查该命令是否一定需要等待结果,并
且等待结果的超时时间是多少,然后启动一个定时器监测该命令的
返回结果,若超时还没有结果,就执行命令超时处理。

2.2 hci分组

《蓝牙协议及其源代码分析》 208页
《bluetooth_Core_v4.2.pdf》779页
《蓝牙协议及其源代码分析》该书可以购买或从网上下载,这里标
志的是网上下载的版本,非完整版本,若对不上号,自己找对应位
置。
《bluetooth_Core_v4.2.pdf》下载地址:
https://www.bluetooth.org/DocMan/handlers/DownloadDoc.ashx?doc_id=286439

2.3 HCI CONFIGURATION PARAMETERS

《bluetooth_Core_v4.2.pdf》787页

2.4 链路控制指令

《蓝牙协议及其源代码分析》 214页
《bluetooth_Core_v4.2.pdf》813页

2.5 链路策略命令

《蓝牙协议及其源代码分析》 216页
《bluetooth_Core_v4.2.pdf》926页

2.6 主机控制器和基带命令

《蓝牙协议及其源代码分析》 216页
《bluetooth_Core_v4.2.pdf》951页

2.7 信息参数命令

《蓝牙协议及其源代码分析》 218页
《bluetooth_Core_v4.2.pdf》1097页

2.8 状态参数命令

《蓝牙协议及其源代码分析》 218页
《bluetooth_Core_v4.2.pdf》1110页

2.9 测试命令

《蓝牙协议及其源代码分析》 218页
《bluetooth_Core_v4.2.pdf》1138页

2.10 事件

《蓝牙协议及其源代码分析》 219页
《bluetooth_Core_v4.2.pdf》1152页

2.11 BLE控制器命令

《bluetooth_Core_v4.2.pdf》1271页

3 ACL链路

L2CAP的通信是基于ACL链路的,两个蓝牙设备之间只有一条ACL链

路,由Connect Handle标识。在进行L2CAP的任何通信之前,都
需要先建立ACL链路,两个蓝牙设备的配对,先建立一条ACL通
码。ACL链路建立后,就会进行L2CAP的一些交互,获取信息,但
需要基于SDP profile获取信息时,L2CAP就会建立一条SDP逻辑链
路(由Channel ID标识),SDP的交互就在L2CAP建立的Channel
上进行,SDP交互完成后,会断开SDP的逻辑链路,若后面没有其
它再需要交互的,ACL链路也会断开,但由于已经配对过,上层会
保存ACL配对的信息。

4 L2CAP层

4.1 l2cap层位置

蓝牙协议栈(Bluetooth stack)简介_第1张图片

4.2 L2CAP包

《bluetooth_Core_v4.2.pdf》1737页
蓝牙协议栈(Bluetooth stack)简介_第2张图片

L2CAP包的类型有CID(Channel ID)确定:
0x0000 Null identifier(不使用)
0x0001 L2CAP Signaling channel(信令信道)
0x0002 Connectionless channel(无连接信道,用于广播)
0x0003 AMP Manager Protocol
0x0004 Attribute Protocol(BLE)
0x0005 Low Energy L2CAP Signaling channel(BLE信令信道)
0x0006 Security Manager Protocol(BLE)
0x0007 BR/EDR Security Manager
0x0008-0x001F Reserved
0x0020-0x003E Assigned Numbers
0x003F AMP Test Manager
0x0040-0xFFFF Dynamically allocated(建立连接后动态分配的信道,承载profile数据)

4.3 CONNECTION-ORIENTED CHANNELS IN BASIC

蓝牙协议栈(Bluetooth stack)简介_第3张图片
L2CAP面向连接的基本帧封包

4.4 CONNECTIONLESS DATA CHANNEL IN BASIC

蓝牙协议栈(Bluetooth stack)简介_第4张图片
L2CAP无连接的帧封包

4.5 CONNECTION-ORIENTED CHANNEL IN RETRANSMISSION/FLOW CONTROL/STREAMING

蓝牙协议栈(Bluetooth stack)简介_第5张图片
L2CAP面向连接的重传、流控帧封包

4.6 CONNECTION-ORIENTED CHANNELS IN LE CREDIT BASED FLOW CONTROL

蓝牙协议栈(Bluetooth stack)简介_第6张图片
L2CAP面向连接信道(BLE使用)

4.7 L2CAP信令封包

《bluetooth_Core_v4.2.pdf》1749页
蓝牙协议栈(Bluetooth stack)简介_第7张图片
L2CAP信令封包,其中infromation payload的内容格式如下:
蓝牙协议栈(Bluetooth stack)简介_第8张图片
Code值:
蓝牙协议栈(Bluetooth stack)简介_第9张图片
蓝牙协议栈(Bluetooth stack)简介_第10张图片

4.8 L2CAP层的状态

《bluetooth_Core_v4.2.pdf》1802页
• CLOSED – channel not connected.
• WAIT_CONNECT – a connection request has been received, but only a
connection response with indication “pending” can be sent.
• WAIT_CONNECT_RSP – a connection request has been sent, pending a
positive connect response.
• CONFIG – the different options are being negotiated for both sides; this
state comprises a number of substates, see Section 6.1.3 on page 114
• OPEN – user data transfer state.
• WAIT_DISCONNECT – a disconnect request has been sent, pending a
disconnect response.
• WAIT_CREATE – a channel creation request has been received, but only a
response with indication “pending” can be sent. This state is similar to
WAIT_CONNECT.
• WAIT_CREATE_RSP – a channel creation request has been sent, pending
a channel creation response. This state is similar to WAIT_CONNECT_RSP.
• WAIT_MOVE – a request to move the current channel to another Controller
has been received, but only a response with indication “pending” can be
sent.
• WAIT_MOVE_RSP – a request to move a channel to another Controller has
been sent, pending a move response
• WAIT_MOVE_CONFIRM – a response to the move channel request has
been sent, waiting for a confirmation of the move operation by the initiator
side
• WAIT_CONFIRM_RSP – a move channel confirm has been sent, waiting for
a move channel confirm response.

各种状态下收到不同事件的处理,L2CAP的重点。

5 Service Discovery Protocol(SDP)

5.1 SDP 协议数据单元( PDU )

《bluetooth_Core_v4.2.pdf》1926页
每一SDP 协议数据单元( PDU )都由PDU 头和PDU 指定参数组
成。报文头包含三个域:协议数据单元ID、事务ID 和参数长度
ParameterLength,参数包括一个后续状态参数。
蓝牙协议栈(Bluetooth stack)简介_第11张图片

5.2 SDP 连接建立过程

蓝牙协议栈(Bluetooth stack)简介_第12张图片

5.3 Service Record

蓝牙协议栈(Bluetooth stack)简介_第13张图片

Each service attribute describes a single characteristic of a service. Some
examples of service attributes are:
蓝牙协议栈(Bluetooth stack)简介_第14张图片

5.4 常见SDP获取的信息

蓝牙协议栈(Bluetooth stack)简介_第15张图片
PnP Information:有的蓝牙耳机没有实现这部分;
蓝牙协议栈(Bluetooth stack)简介_第16张图片

5.5 UUID定义

https://www.bluetooth.com/specifications/assigned-numbers/service-discovery

6 通用访问配置文件(Generic Access Profile, GAP)

GAP是所有其他配置文件的基础,它定义了在蓝牙设备间建立基带链
路的通用方法.除此之外,GAP还定义了下列内容:
必须在所有蓝牙设备中实施的功能
发现和链接设备的通用步骤
基本用户界面术语.
蓝牙协议栈(Bluetooth stack)简介_第17张图片
GAP处理未连接的两个设备间的发现和建立连接过程,GAP确保了
两个蓝牙设备可通过蓝牙技术交换信息。对于GAP的操作,直接由
HCI命令分组控制。

7 RFCOMM协议

7.1 RFCOMM数据封包格式

蓝牙协议栈(Bluetooth stack)简介_第18张图片
Frame Structure for Basic option
蓝牙协议栈(Bluetooth stack)简介_第19张图片
一个封包内的各部分内容
蓝牙协议栈(Bluetooth stack)简介_第20张图片
UIH frames with P/F-bit = 1 and credit based flow control used.
蓝牙协议栈(Bluetooth stack)简介_第21张图片
蓝牙协议栈(Bluetooth stack)简介_第22张图片

8 AUDIO/VIDEO CONTROL TRANSPORT PROTOCOL(AVCTP)

Audio/Video Control Transport Protocol (AVCTP), which is used to transport command and response

messages for controlling Audio Video features in conformant
devices. This protocol enables a device to support more than
one control profile at the same time; each supported profile
shall define its own message formatting and/or usage rules.

8.1 AVCTP所处的位置

蓝牙协议栈(Bluetooth stack)简介_第23张图片
AVCTP协议封包
蓝牙协议栈(Bluetooth stack)简介_第24张图片
蓝牙协议栈(Bluetooth stack)简介_第25张图片

9 AUDIO/VIDEO REMOTE CONTROL PROFILE(AVCRP)

 This profile defines the requirements for Bluetooth® devices necessary for the support of the

Audio/Video Remote Control usage case. The requirements
are expressed in terms of end-user services, and by defining
the features and procedures that are required for
interoperability between Bluetooth devices in the
Audio/Video Remote Control usage case.

9.1 AVRC

蓝牙协议栈(Bluetooth stack)简介_第26张图片

10 Generic Audio/Video Distribution Profile (GAVDP)

  This profile defines the requirements for

Bluetooth® devices necessary to set up streaming channels
used for support of audio/video distribution. The
requirements are expressed in terms of services provided to
applications, and by defining the features and procedures
that are required for interoperability between Bluetooth
devices in the Audio/Video Distribution usage model.

10.1 GAVDP协议位置

蓝牙协议栈(Bluetooth stack)简介_第27张图片

11 AUDIO/VIDEO DISTRIBUTION TRANSPORT PROTOCOL(AVDTP)

 This protocol defines A/V stream negotiation, 

establishment, and transmission procedures. Also specified
are the message formats that are exchanged between such
devices to transport their A/V streams in A/V distribution
applications.

11.1 AVDTP

蓝牙协议栈(Bluetooth stack)简介_第28张图片

11.2 AVDTP一些交互例子

蓝牙协议栈(Bluetooth stack)简介_第29张图片

12 ADVANCED AUDIO DISTRIBUTION PROFILE (A2DP)

12.1 A2DP协议位置

蓝牙协议栈(Bluetooth stack)简介_第30张图片
蓝牙协议栈(Bluetooth stack)简介_第31张图片

13 Hand s-Free Profile(HFP)

 The Hands-Free Profile (HFP) 1.7.1 specification

defines a set of functions such that a Mobile Phone can be
used in conjunction with a Hands-Free device (e.g., installed
in the car or represented by a wearable device such as a
headset), with a Bluetooth Link providing a wireless means
for both remote control of the Mobile Phone by the Hands-
Free device and voice connections between the Mobile
Phone and the Hands-Free device.

13.1 HFP协议位置

蓝牙协议栈(Bluetooth stack)简介_第32张图片
蓝牙协议栈(Bluetooth stack)简介_第33张图片

14 HEADSET PROFILE(HSP)

 This profile defines the requirements for 

Bluetooth® devices necessary to support the Headset use
case. The requirements are expressed in terms of end-user
services, and by defining the features and procedures that are
required for interoperability between Bluetooth devices in the
Headset use case.

14.1 HSP协议位置

蓝牙协议栈(Bluetooth stack)简介_第34张图片
HSP(手机规格)– 提供手机(移动电话)与耳机之间通信所需的基
本功能。
HFP(免提规格)– 在 HSP 的基础上增加了某些扩展功能,原来只
用于从固定车载免提装置来控制移动电话。
A2DP(高级音频传送规格)– 允许传输立体声音频信号。 (相比用
于 HSP 和 HFP 的单声道加密,质量要好得多)。
AVRCP(音频/视频遥控规格)–用于从控制器(如立体声耳机)向
目标设备(如装有 Media Player 的电脑)发送命令(如前跳、暂停
和播放)。

15 Human Interface Device (HID) Profile

 This profile defines how devices with Bluetooth  

wireless communications can use the HID Specification
initially to discover the feature set of a Bluetooth HID device,
and then communicate with the Bluetooth HID device. This
profile further defines how a device with Bluetooth wireless
communications can support HID services over the Bluetooth
protocol stack using the Logical Link Control and Adaptation
Protocol (L2CAP) layer.

15.1 HID协议位置

蓝牙协议栈(Bluetooth stack)简介_第35张图片

15.2 HID head message

蓝牙协议栈(Bluetooth stack)简介_第36张图片

16 Serial Port Profile(SPP)

 This profile defines the requirements for  

Bluetooth® devices necessary for setting up emulated serial
cable connections using RFCOMM between two peer devices. The requirements are expressed in terms of services
provided to applications, and by defining the features and
procedures that are required for interoperability between
Bluetooth devices.

16.1 SPP协议位置

蓝牙协议栈(Bluetooth stack)简介_第37张图片
蓝牙协议栈(Bluetooth stack)简介_第38张图片

你可能感兴趣的:(bluetooth,蓝牙,协议栈)