硬汉嵌入式论坛

 找回密码
 立即注册
查看: 214|回复: 2
收起左侧

[技术讨论] [ESP-IDF学习记录] 二、蓝牙广播ADV

[复制链接]

39

主题

196

回帖

323

积分

高级会员

积分
323
发表于 2024-4-16 11:09:37 | 显示全部楼层 |阅读模式
笔者最近开始接触学习蓝牙,蓝牙核心文档Core_5.2对我来说太长,从实际使用中很可能蓝牙对我来说只是辅助功能(比如说我就用一下蓝牙配网)

我相信很多朋友和我一样,也是这样的需求。对深入了蓝牙协议栈,只求懂个大概,不求深入了解。我就是以这样的需求,这样的视角来写的这篇文章。

由于我也是一个初学者,难免有错漏之处,请大家保持自己的判断力,各种信息还是以官方文档说明为准。




在正式开始之前,来简单讲一下蓝牙的分类,蓝牙分为经典蓝牙和BLE蓝牙。当然还有双模的,双模就是两者共存,根据自己需要选择其中一种。
以乐鑫的ESP系列来看,只有ESP32是双模的,其他的例如C3,S3都只有BLE。S2甚至把蓝牙给砍掉了,就不包含蓝牙的功能。

这两者的区别就是功耗和数据速率。一般来说像是蓝牙音箱这种功能就需要用经典蓝牙,而一些数据不大的,对功耗有要求的就选择BLE。(不过据说BLE的LE Audio也是可以传输音频的,出来也有一段时间了)
未来的趋势也是BLE的速率会越来越快吧。

我这里就只接触BLE,对于我来说这个开发相对比较简单一点,功能也能满足





从蓝牙架构上来说,因为蓝牙是应手机而生的,最初做出来就是为了解决手机连接的问题的。
一种常见的架构就是HOST+CONTROLLER。
HOST就是指手机的芯片,CONTROLLER就是蓝牙芯片。HOST根据一个标准的协议HCI,去控制CONTROLLER芯片。这样做的好处就是更换HOST或者更换蓝牙芯片,对程序没有影响,完全可以兼容。

第二种就是单颗芯片整体方案。
例如我们的乐鑫ESP32就是这样啦,HOST和CONTROLLER在同一颗芯片上。不过ESP32也可以配置成工作在CONTROLLER,然后串口引出来,让别的MCU通过HCI控制它。当成第一种方法来使用。

第三种就是非标准的HCI协议,这里不涉及。


我们来看一下ESP32的蓝牙协议栈,它支持两个协议栈。
一个是乐鑫魔改的安卓bluedroid,支持经典蓝牙和BLE。
另外一个是开源的NimBLE,仅支持BLE。

协议栈的具体内容,笔者都看不懂,因为看这个首先要对蓝牙协议一点了解,我还是初学阶段。
关于这部分可以参考https://www.espressif.com.cn/sites/default/files/documentation/esp32_bluetooth_architecture_cn.pdf


我们就先来看看怎么让手机能搜索到蓝牙。

手机上安装一个蓝牙调试工具,笔者用的是IOS上的NRF Connect。

直接运行烧录一下examples/bluetooth/bluedroid/ble/gatt_server_service_table这个例程。

我们就可以直接搜索到ESP_GATTS_DEMO这个蓝牙设备了。

BLE广播流程.png

蓝牙的广播流程图上图。
这里存在两个角色:Advertiser和Scanner。

在本次的应用过程中。
ADV就是我们的板子,而Scanner就是手机。

蓝牙协议里的Generic Access Profile(GAP)就是描述这个过程的规范。

广播启动后,设备会一直在发ADV_IND包。

这个广播包的长度最长为31字节,其中的数据格式如下,有点类似TLV格式,:
ADV数据格式.png

对照实际代码来看,第一行就对应格式里的length,第二行就是AD Type,后面的就是内容:
esp蓝牙广播具体代码.png

我们只要知道AD Type的含义就可以自己构建符合规范的广播包了。
AD type这个文档就在Assigned_Numbers的2.3章
https://bitbucket.org/bluetooth-SIG/public/src/main/assigned_numbers/core/ad_types.yaml

[XML] 纯文本查看 复制代码
# This document, regardless of its title or content, is not a Bluetooth
# Specification as defined in the Bluetooth Patent/Copyright License Agreement
# (“PCLA”) and Bluetooth Trademark License Agreement. Use of this document by
# members of Bluetooth SIG is governed by the membership and other related
# agreements between Bluetooth SIG Inc. (“Bluetooth SIG”) and its members,
# including the PCLA and other agreements posted on Bluetooth SIG’s website
# located at [url=http://www.bluetooth.com.]www.bluetooth.com.[/url]
# 
# THIS DOCUMENT IS PROVIDED “AS IS” AND BLUETOOTH SIG, ITS MEMBERS, AND THEIR
# AFFILIATES MAKE NO REPRESENTATIONS OR WARRANTIES AND DISCLAIM ALL WARRANTIES,
# EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, TITLE,
# NON-INFRINGEMENT, FITNESS FOR ANY PARTICULAR PURPOSE, THAT THE CONTENT OF THIS
# DOCUMENT IS FREE OF ERRORS.
# 
# TO THE EXTENT NOT PROHIBITED BY LAW, BLUETOOTH SIG, ITS MEMBERS, AND THEIR
# AFFILIATES DISCLAIM ALL LIABILITY ARISING OUT OF OR RELATING TO USE OF THIS
# DOCUMENT AND ANY INFORMATION CONTAINED IN THIS DOCUMENT, INCLUDING LOST REVENUE,
# PROFITS, DATA OR PROGRAMS, OR BUSINESS INTERRUPTION, OR FOR SPECIAL, INDIRECT,
# CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF
# THE THEORY OF LIABILITY, AND EVEN IF BLUETOOTH SIG, ITS MEMBERS, OR THEIR
# AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
# 
# This document is proprietary to Bluetooth SIG. This document may contain or
# cover subject matter that is intellectual property of Bluetooth SIG and its
# members. The furnishing of this document does not grant any license to any
# intellectual property of Bluetooth SIG or its members.
# 
# This document is subject to change without notice.
# 
# Copyright © 2020–2024 by Bluetooth SIG, Inc. The Bluetooth word mark and logos
# are owned by Bluetooth SIG, Inc. Other third-party brands and names are the
# property of their respective owners.

ad_types:
 - value: 0x01
   name: Flags
   reference: Core Specification Supplement, Part A, Section 1.3
 - value: 0x02
   name: Incomplete List of 16-bit Service Class UUIDs
   reference: Core Specification Supplement, Part A, Section 1.1
 - value: 0x03
   name: Complete List of 16-bit Service Class UUIDs
   reference: Core Specification Supplement, Part A, Section 1.1
 - value: 0x04
   name: Incomplete List of 32-bit Service Class UUIDs
   reference: Core Specification Supplement, Part A, Section 1.1
 - value: 0x05
   name: Complete List of 32-bit Service Class UUIDs
   reference: Core Specification Supplement, Part A, Section 1.1
 - value: 0x06
   name: Incomplete List of 128-bit Service Class UUIDs
   reference: Core Specification Supplement, Part A, Section 1.1
 - value: 0x07
   name: Complete List of 128-bit Service Class UUIDs
   reference: Core Specification Supplement, Part A, Section 1.1
 - value: 0x08
   name: Shortened Local Name
   reference: Core Specification Supplement, Part A, Section 1.2
 - value: 0x09
   name: Complete Local Name
   reference: Core Specification Supplement, Part A, Section 1.2
 - value: 0x0A
   name: Tx Power Level
   reference: Core Specification Supplement, Part A, Section 1.5
 - value: 0x0D
   name: Class of Device
   reference: Core Specification Supplement, Part A, Section 1.6
 - value: 0x0E
   name: Simple Pairing Hash C-192
   reference: Core Specification Supplement, Part A, Section 1.6
 - value: 0x0F
   name: Simple Pairing Randomizer R-192
   reference: Core Specification Supplement, Part A, Section 1.6
 - value: 0x10
   name: Device ID
   reference: Device ID Profile (when used in EIR data)
 - value: 0x10
   name: Security Manager TK Value
   reference: Core Specification Supplement, Part A, Section 1.8 (when used in OOB data blocks)
 - value: 0x11
   name: Security Manager Out of Band Flags
   reference: Core Specification Supplement, Part A, Section 1.7
 - value: 0x12
   name: Peripheral Connection Interval Range
   reference: Core Specification Supplement, Part A, Section 1.9
 - value: 0x14
   name: List of 16-bit Service Solicitation UUIDs
   reference: Core Specification Supplement, Part A, Section 1.10
 - value: 0x15
   name: List of 128-bit Service Solicitation UUIDs
   reference: Core Specification Supplement, Part A, Section 1.10
 - value: 0x16
   name: Service Data - 16-bit UUID
   reference: Core Specification Supplement, Part A, Section 1.11
 - value: 0x17
   name: Public Target Address
   reference: Core Specification Supplement, Part A, Section 1.13
 - value: 0x18
   name: Random Target Address
   reference: Core Specification Supplement, Part A, Section 1.14
 - value: 0x19
   name: Appearance
   reference: Core Specification Supplement, Part A, Section 1.12
 - value: 0x1A
   name: Advertising Interval
   reference: Core Specification Supplement, Part A, Section 1.15
 - value: 0x1B
   name: LE Bluetooth Device Address
   reference: Core Specification Supplement, Part A, Section 1.16
 - value: 0x1C
   name: LE Role
   reference: Core Specification Supplement, Part A, Section 1.17
 - value: 0x1D
   name: Simple Pairing Hash C-256
   reference: Core Specification Supplement, Part A, Section 1.6
 - value: 0x1E
   name: Simple Pairing Randomizer R-256
   reference: Core Specification Supplement, Part A, Section 1.6
 - value: 0x1F
   name: List of 32-bit Service Solicitation UUIDs
   reference: Core Specification Supplement, Part A, Section 1.10
 - value: 0x20
   name: Service Data - 32-bit UUID
   reference: Core Specification Supplement, Part A, Section 1.11
 - value: 0x21
   name: Service Data - 128-bit UUID
   reference: Core Specification Supplement, Part A, Section 1.11
 - value: 0x22
   name: LE Secure Connections Confirmation Value
   reference: Core Specification Supplement, Part A, Section 1.6
 - value: 0x23
   name: LE Secure Connections Random Value
   reference: Core Specification Supplement, Part A, Section 1.6
 - value: 0x24
   name: URI
   reference: Core Specification Supplement, Part A, Section 1.18
 - value: 0x25
   name: Indoor Positioning
   reference: Indoor Positioning Service
 - value: 0x26
   name: Transport Discovery Data
   reference: Transport Discovery Service
 - value: 0x27
   name: LE Supported Features
   reference: Core Specification Supplement, Part A, Section 1.19
 - value: 0x28
   name: Channel Map Update Indication
   reference: Core Specification Supplement, Part A, Section 1.20
 - value: 0x29
   name: PB-ADV
   reference: Mesh Profile Specification, Section 5.2.1
 - value: 0x2A
   name: Mesh Message
   reference: Mesh Profile Specification, Section 3.3.1
 - value: 0x2B
   name: Mesh Beacon
   reference: Mesh Profile Specification, Section 3.9
 - value: 0x2C
   name: BIGInfo
   reference: Core Specification Supplement, Part A, Section 1.21
 - value: 0x2D
   name: Broadcast_Code
   reference: Core Specification Supplement, Part A, Section 1.22
 - value: 0x2E
   name: Resolvable Set Identifier
   reference: Coordinated Set Identification Profile v1.0 or later
 - value: 0x2F
   name: Advertising Interval - long
   reference: Core Specification Supplement, Part A, Section 1.15
 - value: 0x30
   name: Broadcast_Name
   reference: Public Broadcast Profile v1.0 or later
 - value: 0x31
   name: Encrypted Advertising Data
   reference: Core Specification Supplement, Part A, Section 1.23
 - value: 0x32
   name: Periodic Advertising Response Timing Information
   reference: Core Specification Supplement, Part A, Section 1.24
 - value: 0x34
   name: Electronic Shelf Label
   reference: ESL Profile
 - value: 0x3D
   name: 3D Information Data
   reference: 3D Synchronization Profile
 - value: 0xFF
   name: Manufacturer Specific Data
   reference: Core Specification Supplement, Part A, Section 1.4


它这里列出了在Core Specification Supplement具体章节可以参考相关含义。文档的链接在这里Core Specification Supplement




评分

参与人数 1金币 +100 收起 理由
eric2013 + 100 很给力!

查看全部评分

回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106721
QQ
发表于 2024-4-17 11:05:12 | 显示全部楼层
好帖,谢谢分享。
回复

使用道具 举报

0

主题

29

回帖

29

积分

新手上路

积分
29
发表于 2024-4-17 19:09:06 | 显示全部楼层
写得不错,请继续。  
我也做过一个蓝牙广播的程序,是在 nRF52810 上运行的。但对 BLE 的内部协议还是不太明白。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-1 04:57 , Processed in 0.285823 second(s), 30 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表