BLE开发基础_01_BLE协议栈_GAP&GATT

本文基于ch585 evt示例代码学习蓝牙开发相关内容


经典蓝牙与BLE

  • 经典蓝牙具有较高的数据速率能力,适用于流媒体、高带宽文件传输和耳机。它有79个RF信道,其中32个广播信道,以便其他设备可以发现和连接。
  • 低功耗蓝牙:针对数据传输不频繁的低功耗应用,例如传 感器和其他低带宽传输。它有40个RF信道,其中3个信道(37/38/39)用于设备发现。
    ee9cb36f-7739-48a8-9b69-13780c18f5f8

BLE信道

------------- ## 典型BLE应用
  • 29494648-fbf4-4c1b-bd82-137e4a92f03a
  • 主机(Central)和从机(Peripheral)在建立连接前,从机通过广播宣告自身存在,主机主动扫描可用从机,随后建立连接,通过BLE协议栈不同层来传输数据

BLE协议栈架构

82ee31bb-e873-4b52-95a2-75f0cf68967d

  • BLE协议栈架构通常分为三个主要层级:应用层(APP)、主机层(HOST)和控制器层(CONTROLLER)。应用层是协议栈的最顶层。BLE设备上运行的应用程序在这一层利用和处理实际数据。主机层位于协议栈中的应用层和控制器层之间,实现了BLE通信所需的所有较高级别协议和配置文件,它还提供高级应用程序编程接口(API),使得应用程序能够与协议栈的较低层交互。控制器层是BLE协议栈的硬件部分,负责蓝牙信号的发送和接收。控制器层处理信号的跳频、调制和解调等任务

GATT-通用属性规范

  • 应用层用于实现BLE设备具体应用需求。应用层通过GATT(通用属性规范)与协议栈下层进行交互,基于GATT定义的服务、特征和相应数据。GATT层供应用程序在两个连接设备之间进行数据通讯,数据以特征的形式传递和储存。
    具体配置以peripheral示例代码为例:
     // Simple Profile Service
    {
        {ATT_BT_UUID_SIZE, primaryServiceUUID}, /* type */
        GATT_PERMIT_READ,                       /* permissions */
        0,                                      /* handle */
        (uint8_t *)&simpleProfileService        /* pValue */
    },
 // Characteristic 1 Declaration
    {
        {ATT_BT_UUID_SIZE, characterUUID},
        GATT_PERMIT_READ,
        0,
        &simpleProfileChar1Props},

    // Characteristic Value 1
    {
        {ATT_BT_UUID_SIZE, simpleProfilechar1UUID},
        GATT_PERMIT_READ | GATT_PERMIT_WRITE,
        0,
        simpleProfileChar1},

    // Characteristic 1 User Description
    {
        {ATT_BT_UUID_SIZE, charUserDescUUID},
        GATT_PERMIT_READ,
        0,
        simpleProfileChar1UserDesp},

有三部分:

  • 服务声明:pValue 指向的是这个服务本身的 UUID(比如 0xFFF0),让主机知道这是一个什么服务。
  • 特征声明:pValue 指向 simpleProfileChar1Props(如 Read/Write 标志位),告诉主机接下来的那个条目支持哪些操作(读/写/....)
// GATT Characteristic Properties Bit Fields
#define GATT_PROP_BCAST                 0x01 //!< Permits broadcasts of the Characteristic Value
#define GATT_PROP_READ                  0x02 //!< Permits reads of the Characteristic Value
#define GATT_PROP_WRITE_NO_RSP          0x04 //!< Permits writes of the Characteristic Value without response
#define GATT_PROP_WRITE                 0x08 //!< Permits writes of the Characteristic Value with response
#define GATT_PROP_NOTIFY                0x10 //!< Permits notifications of a Characteristic Value without acknowledgement
#define GATT_PROP_INDICATE              0x20 //!< Permits indications of a Characteristic Value with acknowledgement
#define GATT_PROP_AUTHEN                0x40 //!< Permits signed writes to the Characteristic Value
#define GATT_PROP_EXTENDED              0x80 //!< Additional characteristic properties are defined in the Characteristic Extended Properties Descriptor

  • 特征值:pValue 指向程序里的变量(如 simpleProfileChar1)。主机读取数据时,读取的就是这个指针指向的内存。
  • 用户描述:给这个特征起个“绰号”(通常是一个字符串,如 "Voltage")

相关函数

  • 初始化客户端特征配置
    // Initialize Client Characteristic Configuration attributes
    GATTServApp_InitCharCfg(INVALID_CONNHANDLE, simpleProfileChar4Config);
  • 将服务注册到GATT服务器
/**
 * @brief   Register a service's attribute list and callback functions with
 *          the GATT Server Application.
 *
 * @param   pAttrs - Array of attribute records to be registered
 * @param   numAttrs - Number of attributes in array
 * @param   encKeySize - Minimum encryption key size required by service (7-16 bytes)
 * @param   pServiceCBs - Service callback function pointers
 *
 * @return  SUCCESS: Service registered successfully.<BR>
 *          INVALIDPARAMETER: Invalid service fields.<BR>
 *          FAILURE: Not enough attribute handles available.<BR>
 *          bleMemAllocError: Memory allocation error occurred.<BR>
 *          bleInvalidRange: Encryption key size's out of range.<BR>
 */
extern bStatus_t GATTServApp_RegisterService( gattAttribute_t *pAttrs, uint16_t numAttrs,
    uint8_t encKeySize, gattServiceCBs_t *pServiceCBs );

GAP-通用访问配置文件

  • GAP定义了广播(Advertiser)、扫描(Scanner)、发起连接(Initiator)等过程,决定了设备如何被其他设备发现,以及如何建立连接。
  • GAP 的核心作用有4个:

(A)模式与过程 (Modes and Procedures): 设备如何被发现(广播)以及如何发现别人(扫描)。
(B)角色 (Roles): 定义设备在蓝牙网络中的身份(如手机是 Central,手环是 Peripheral)。
(C)安全 (Security): 定义安全级别和配对模式的基础。
(D)数据格式 (Data Formats): 定义广播数据和扫描响应数据的通用格式。

相关配置

角色与连接参数(GAPRole_SetParameter):

  • 广播使能
  • 广播数据
  • 扫描应答数据
  • 最小/最大连接间隔

广播频率与扫描通知(GAP_SetParamValue):

  • 广播时间间隔
  • 扫描请求通知

绑定管理与安全配置(GAPBondMgr_SetParameter):

  • 默认配对码
  • 配对模式
  • 中间人攻击保护
  • IO能力
    // Setup the GAP Peripheral Role Profile
    {
        uint8_t  initial_advertising_enable = TRUE;
        uint16_t desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
        uint16_t desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;

        // Set the GAP Role Parameters
        GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);
        GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData);
        GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);
        GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &desired_min_interval);
        GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &desired_max_interval);
    }

    {        uint16_t advInt = DEFAULT_ADVERTISING_INTERVAL;

        // Set advertising interval
        GAP_SetParamValue(TGAP_DISC_ADV_INT_MIN, advInt);
        GAP_SetParamValue(TGAP_DISC_ADV_INT_MAX, advInt);

        // Enable scan req notify
        GAP_SetParamValue(TGAP_ADV_SCAN_REQ_NOTIFY, ENABLE);
    }
posted @ 2026-03-30 17:25  J_02346  阅读(59)  评论(0)    收藏  举报