ESP32基于GATT Client蓝牙网关 (一主多从)

本文详细介绍ESP32作为GATTclient在多连接应用中的配置与操作,包括设置扫描参数、连接设备、服务注册及MTU管理,适合理解和移植到多设备蓝牙项目中。
前言

对于传统的BLE低功耗设备,通过手机ble作为主机角色进行通信。如果需要主动接入网络,通过esp32作为主机,采用一主多从的应用可以并行多台接入,本章主要针对Multi-connection的应用例程展开讲解

ESP32工作在GATT client的角色,扫描并连接GATT server的广播设备

如有异议,欢迎留言指正

模型架构

基于BLE GATT直连的方案,主从拓扑结构,通信消息统一由网关主机处理,组网方式为星型网络
在这里插入图片描述

一对多连接配置

IDF为每个BLE服务创建一个单独的配置文件,应用配置文件以ID号单独定义
在这里插入图片描述

配置流程

主要步骤

  • 系统初始化
  • 扫描参数配置
  • 发现附近的设备
  • 与匹配的设备进行连接
  • 注册服务通知 register notify
    在这里插入图片描述
修改最大连接数

进入配置工具菜单idf.py menuconfig中选择,进入Component config->Bluetooth->Bluedroid Options->BT/BLE MAX ACL CONNECTIONS
在这里插入图片描述

常用API
GAP

api头文件路径components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h

  • esp_err_t esp_ble_gap_register_callback(esp_gap_ble_cb_t callback):注册GAP事件回调
    • callback:回调函数
  • esp_err_t esp_ble_gap_set_scan_params(esp_ble_scan_params_t *scan_params):设置扫描参数
    • *scan_params:配置扫描类型、地址类型、滤波类型、间隔、窗口等
  • esp_err_t esp_ble_gap_start_scanning(uint32_t duration):启动扫描
    • duration:扫描时间,单位sec;写0为持续扫描
  • esp_err_t esp_ble_gap_stop_scanning(void):停止扫描
  • esp_err_t esp_ble_gap_read_rssi(esp_bd_addr_t remote_addr):读取RSSI信号强度,结果通过ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT事件触发返回
  • esp_err_t esp_ble_gap_disconnect(esp_bd_addr_t remote_device):断开连接
    • remote_device:断开peer目标地址

更多内容请参考GAP API

GATTC

api头文件路径components/bt/host/bluedroid/api/include/api/esp_gattc_api.h

  • esp_err_t esp_ble_gattc_register_callback(esp_gattc_cb_tcallback):注册GATTC事件回调
    • callback:回调函数指针
  • esp_err_t esp_ble_gattc_app_register(uint16_t app_id): 注册GATT服务应用
  • esp_err_t esp_ble_gattc_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct):打开连接
    • gattc_if:gattc访问接口
    • remote_bda:远程设备地址
    • remote_addr_type:远程设备地址类型
    • is_direct:true - 直接连接;false - 后台连接
  • esp_err_t esp_ble_gattc_close(esp_gatt_if_t gattc_if, uint16_t conn_id):关闭与gatt服务端连接
    • gattc_if:gattc访问接口
    • conn_id:连接id
  • esp_err_t esp_ble_gattc_send_mtu_req(esp_gatt_if_t gattc_if, uint16_t conn_id):设置通道MTU大小
    • gattc_if:gattc访问接口
    • conn_id:连接id
  • esp_err_t esp_ble_gattc_search_service(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_bt_uuid_t *filter_uuid):查找指定服务,仅本地缓存中查找匹配
    • gattc_if:gattc访问接口
    • conn_id:连接id
    • filter_uuid:指定服务uuid
  • esp_gatt_status_t esp_ble_gattc_get_attr_count(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_gatt_db_attr_type_t type, uint16_t start_handle, uint16_t end_handle, uint16_t char_handle, uint16_t *count):获取指定服务或特征的属性个数
    • gattc_if:gattc访问接口
    • conn_id:连接id
    • type:属性类型
    • start_handle:属性起始句柄
    • end_handle:属性结束句柄
    • char_handle:特征句柄
    • count:输出查找匹配属性类型的个数
  • esp_gatt_status_t esp_ble_gattc_get_char_by_uuid(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t start_handle, uint16_t end_handle, esp_bt_uuid_t char_uuid, esp_gattc_char_elem_t *result, uint16_t *count):获取指定特征UUID的特征,从本地缓存中获取
    • gattc_if:gattc访问接口
    • conn_id:连接id句柄
    • start_handle:属性起始句柄
    • end_handle:属性结束句柄
    • char_uuid:查找的特征uuid
    • result:输出匹配特征的指针
    • count:输入需要查找的特征数,并输出已经获取到的特征数
  • esp_gatt_status_t esp_ble_gattc_get_descr_by_uuid(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t start_handle, uint16_t end_handle, esp_bt_uuid_t char_uuid, esp_bt_uuid_t descr_uuid, esp_gattc_descr_elem_t *result, uint16_t *count):获取具有给定特征uuid的描述符
    • gattc_if:gattc访问接口
    • conn_id:连接id句柄
    • start_handle:属性起始句柄
    • end_handle:属性结束句柄
    • char_uuid:特征uuid
    • descr_uuid:描述符uuid
    • result:输出匹配特征描述符的指针
    • count:输入需要查找的描述符数,并输出已经查找到的描述符数量
  • esp_err_t esp_ble_gattc_write_char(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t handle, uint16_t value_len, uint8_t *value, esp_gatt_write_type_t write_type, esp_gatt_auth_req_t auth_req):gattc写入特征值
    • gattc_if:gattc访问接口
    • conn_id:连接id
    • handle:特征句柄
    • value_len:写入数据长度
    • value:写入数据缓存
    • write_type:写属性类型(需要应答、无需应答)
    • auth_req:认证请求(0 - 无需认证)
  • esp_err_t esp_ble_gattc_write_char_descr(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t handle, uint16_t value_len, uint8_t *value, esp_gatt_write_type_t write_type, esp_gatt_auth_req_t auth_req):gattc写入特征描述符值
    • gattc_if:gattc访问接口
    • conn_id:连接id
    • handle:描述符句柄
    • value_len:写入数据长度
    • value:写入数据缓存
    • write_type:写属性类型(需要应答、无需应答)
    • auth_req:认证请求(0 - 无需认证)
  • esp_err_t esp_ble_gattc_register_for_notify(esp_gatt_if_t gattc_if, esp_bd_addr_t server_bda, uint16_t handle):注册服务通知
    • gattc_if:gattc访问接口
    • server_bda:服务端地址
    • handle:服务特征句柄

详情内容请参考GATT CLIENT API

代码讲解

示例demo,目录examples/bluetooth/bluedroid/ble/gattc_multi_connect,例程为BLE GATT基础上,实现一个GATT客户端同时连接到三个不同的GATT服务端设备

入口app_main

app_main()中进行了蓝牙协议栈初始化与回调事件注册

esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg); //初始化蓝牙控制器
if (ret) {
   
   
    ESP_LOGE(GATTC_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
    return;
}
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);//使能BLE模式
if (ret) {
   
   
    ESP_LOGE(GATTC_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
    return;
}
ret = esp_bluedroid_init();//初始化与分配资源
if (ret) {
   
   
    ESP_LOGE(GATTC_TAG, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
    return;
}
ret = esp_bluedroid_enable();//使能蓝牙
if (ret) {
   
   
    ESP_LOGE(GATTC_TAG, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
    return;
}
//register the  callback function to the gap module
ret = esp_ble_gap_register_callback(esp_gap_cb); //注册gap事件回调
if (ret){
   
   
    ESP_LOGE(GATTC_TAG, "gap register error, error code = %x", ret);
    return;
}
//register the callback function to the gattc module
ret = esp_ble_gattc_register_callback(esp_gattc_cb
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值