项目环境
项目 | 版本 |
---|---|
ESP-IDF | v5.1.1 |
开发板 | ESP32-WROOM-32 |
example project | gatt_server_service_table |
Visual Stdio Code | April 2023 (version 1.78) |
添加Service
1.添加宏定义配置
// 日志前缀
#define GATTS_TABLE_TAG "GATTS_TABLE"
#define GATTS_MY_TAG "GATTS_MY_TABLE" //自定义Tag
#define PROFILE_NUM 2 //修改Service总数
#define PROFILE_APP_IDX 0 //Profile索引
#define PROFILE_MY_APP_IDX 1 //自定义Profile索引
#define SVC_INST_ID 0 //服务启动ID
#define SVC_INST_MY_ID 1 //自定义服务启动ID
//句柄
uint16_t heart_rate_handle_table[HRS_IDX_NB];
uint16_t my_handle_table[MY_LENGTH]; //自定义句柄
//gatt状态机回调函数
static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
static void gatts_my_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param); //自定义回调函数声明
//装入自定义回调函数
static struct gatts_profile_inst heart_rate_profile_tab[PROFILE_NUM] = {
[PROFILE_APP_IDX] = {
.gatts_cb = gatts_profile_event_handler,
.gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
},
[PROFILE_MY_APP_IDX] = {
.gatts_cb = gatts_my_profile_event_handler,
.gatts_if = ESP_GATT_IF_NONE,
},
};
//UUID配置
static const uint16_t GATTS_SERVICE_UUID_TEST = 0x00FF;
static const uint16_t GATTS_CHAR_UUID_TEST_A = 0xFF01;
static const uint16_t GATTS_CHAR_UUID_TEST_B = 0xFF02;
static const uint16_t GATTS_CHAR_UUID_TEST_C = 0xFF03;
static const uint16_t GATTS_SERVICE_UUID_MY = 0x0077; //自定义Service UUID
static const uint16_t GATTS_SERVICE_UUID_MY_A = 0x7701; //自定义Characteristic UUID
//项目额外配置
static const uint8_t char_prop_read_write = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE;
static const uint8_t my_value[4] = {
0x11, 0x85, 0x35, 0x95};
2. 添加Service结构
Service Declaration
— Characteristic Declaration
— Characteristic Value
— Characteristic Description
在gatts_table_creat_demo.h内定义以下枚举类,该枚举类对应在调试软