后文内容摘自于《Bluetooth Essentials for Programmers》一书。
0、蓝牙编程总体过程与IP网络编程类比
image.pngimage.png
1、Discoverability and Connectability
发现能力与连接能力,即设备能否被扫描发现,能否进行连接
image.pngInquiry Scan 表示设备能否被发现的;Page Scan表示设备会不会接受其它设备的连接请求
注意:蓝牙并不是主动广播自己的存在,然后被其它设备发现,而是要主动发起设备发现查询,收到其它设备的发现应答,才能找到周围的设备
Note: A common misconception is that when a Bluetooth device enters an area, it somehow “announces” its presence so that
other devices will know that it’s around. This never happens (even
though it’s not a bad idea), and the only way for one device to detect the presence of another is to conduct a device discovery.
2、蓝牙中涉及的各种传输协议简要说明
-
RFCOMM (Radio Frequency Communications protocol, a reliable
streams-based protocol)
用蓝牙通信技术模拟传统的RS-232标准的串口协议,端口 30
注意此处端口的含义理解和IP网络应用层的协议端口号,完全不是一回事,见后文中端口章节说明
有些蓝牙库只支持该协议的应用是因为通常蓝牙库能支持这个协议,就能满足大多数蓝牙应用 -
L2CAP (Logical Link Control and Adaption Protocol, a packet-based
protocol that can be configured with varying levels of reliability )There are three possible retransmit policies: • never retransmit (but make a best effort); • always retransmit until success or total connection failure (reliable, the default); and • drop a packet and move on to queued data if a packet hasn’t been acknowledged after a specified time limit (0–1279 ms). This is useful when data must be transmitted in a timely manner (and it assumes a best effort
L2CAP协议的缺陷,单个连接关于L2CAP的修改会对所有的L2CAP连接都产生影响;
所有的RCOMM都是封装在L2CAP中 -
ACL (The Asynchronous Connection-oriented Logical (ACL) transport protocol)
不会再在编程时直接接触,所有的L2CAP是封装在ACL中 -
SCO (Synchronous ConnectionOriented (SCO) logical transport protocol)
image.png
用于传输高清音频的,一个设备可以有三条该类型的连接,两个设备间最多一个该连接,实际应用中一般最多只有一条该连接
3、蓝牙编程中的端口
A port is used to allow multiple applications on the same device to simultaneously utilize the same
transport protocol. Almost all Internet transport protocols in common usage are designed with the
notion of port numbers.Bluetooth is no exception, but uses slightly different terminology. In L2CAP,
ports are called Protocol Service Multiplexers (PSM), and can take on oddnumbered values between 1
and 32,767. Don’t ask why they have to be odd-numbered values, because you probably won’t get a
convincing answer. In RFCOMM, channels 1–30 are available for use. Throughout the rest of
this book, the word port is used instead of protocol service multiplexer and
channel, mostly for clarity.
端口在不同的传输协议下有不同的含义,可能是指PSM 或者信道的ID编号
关于PSM 和 CID的理解区分,见 c - Bluetooth LE L2CAP CID vs. PSM - Stack Overflow
4、Service Discovery Protocol
更上层的协议,偏与实际应用相关
image.png
image.png
4、Bluetooth Profiles + RFCs
常见的profile,可在蓝牙技术官网查看更多细节 http://www.bluetooth.com/Bluetooth/Learn/Technology/Specifications/
5、Host Controller Interface
image.png6、蓝牙协议栈
image.png image.png蓝牙 参考资料:
链接:https://pan.baidu.com/s/1wKeFFJNI6TbZoUesqkby7g
提取码:f5ud
网友评论