mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
component/bt : modify demo for new api
1. modify GATT Client demo 2. modify GATT Server demo 3. fix a register notify bug
This commit is contained in:
parent
c01dedcb06
commit
65fe6ab320
@ -422,7 +422,7 @@ static void btc_gattc_reg_for_notify(btc_ble_gattc_args_t *arg)
|
||||
memset(¶m, 0, sizeof(esp_ble_gattc_cb_param_t));
|
||||
param.reg_for_notify.status = status;
|
||||
memcpy(¶m.reg_for_notify.srvc_id, &arg->reg_for_notify.service_id, sizeof(esp_gatt_srvc_id_t));
|
||||
memcpy(¶m.reg_for_notify.char_id, &arg->reg_for_notify.service_id, sizeof(esp_gatt_id_t));
|
||||
memcpy(¶m.reg_for_notify.char_id, &arg->reg_for_notify.char_id, sizeof(esp_gatt_id_t));
|
||||
BTC_GATTC_CB_TO_APP(ESP_GATTC_REG_FOR_NOTIFY_EVT, arg->reg_for_notify.gattc_if, ¶m);
|
||||
}
|
||||
|
||||
|
@ -31,15 +31,20 @@
|
||||
#include "esp_bt_main.h"
|
||||
|
||||
///Declare the static function
|
||||
static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *parama);
|
||||
static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
|
||||
static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
|
||||
|
||||
#define GATTS_SERVICE_UUID_TEST_A 0x00FF
|
||||
#define GATTS_CHAR_UUID_TEST_A 0xFF01
|
||||
#define GATTS_DESCR_UUID_TEST_A 0x3333
|
||||
#define GATTS_NUM_HANDLE_TEST_A 4
|
||||
|
||||
#define GATTS_SERVICE_UUID_TEST_B 0x00EE
|
||||
#define GATTS_CHAR_UUID_TEST_B 0xEE01
|
||||
#define GATTS_DESCR_UUID_TEST_B 0x2222
|
||||
#define GATTS_NUM_HANDLE_TEST_B 4
|
||||
|
||||
#define GATTS_SERVICE_UUID_TEST 0x00FF
|
||||
#define GATTS_CHAR_UUID_TEST 0xFF01
|
||||
#define GATTS_DESCR_UUID_TEST 0x3333
|
||||
#define APP_ID_TEST 0x18
|
||||
#define GATTS_NUM_HANDLE_TEST 4
|
||||
#define TEST_DEVICE_NAME "ESP_GATTS_DEMO"
|
||||
|
||||
#define TEST_MANUFACTURER_DATA_LEN 17
|
||||
static uint8_t test_service_uuid128[32] = {
|
||||
/* LSB <--------------------------------------------------------------------------------> MSB */
|
||||
@ -103,7 +108,7 @@ static struct gatts_profile_inst gl_profile_tab[PROFILE_NUM] = {
|
||||
.gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
|
||||
},
|
||||
[PROFILE_B_APP_ID] = {
|
||||
.gatts_cb = NULL, /* This demo does not implement, similar as profile A */
|
||||
.gatts_cb = gatts_profile_b_event_handler, /* This demo does not implement, similar as profile A */
|
||||
.gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
|
||||
},
|
||||
};
|
||||
@ -128,7 +133,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.is_primary = true;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.inst_id = 0x00;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.len = ESP_UUID_LEN_16;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.uuid.uuid16 = GATTS_SERVICE_UUID_TEST;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.uuid.uuid16 = GATTS_SERVICE_UUID_TEST_A;
|
||||
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
|
||||
@ -136,7 +141,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
esp_ble_gap_config_adv_data(&test_adv_data);
|
||||
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_A_APP_ID].service_id, GATTS_NUM_HANDLE_TEST);
|
||||
esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_A_APP_ID].service_id, GATTS_NUM_HANDLE_TEST_A);
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
break;
|
||||
case ESP_GATTS_READ_EVT: {
|
||||
@ -168,7 +173,105 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
LOG_INFO("CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle);
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_handle = param->create.service_handle;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.len = ESP_UUID_LEN_16;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST_A;
|
||||
|
||||
esp_ble_gatts_start_service(gl_profile_tab[PROFILE_A_APP_ID].service_handle);
|
||||
|
||||
esp_ble_gatts_add_char(gl_profile_tab[PROFILE_A_APP_ID].service_handle, &gl_profile_tab[PROFILE_A_APP_ID].char_uuid,
|
||||
ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
|
||||
ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY);
|
||||
break;
|
||||
case ESP_GATTS_ADD_INCL_SRVC_EVT:
|
||||
break;
|
||||
case ESP_GATTS_ADD_CHAR_EVT:
|
||||
LOG_INFO("ADD_CHAR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
||||
param->add_char.status, param->add_char.attr_handle, param->add_char.service_handle);
|
||||
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_handle = param->add_char.attr_handle;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].descr_uuid.len = ESP_UUID_LEN_16;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].descr_uuid.uuid.uuid16 = ESP_GATT_UUID_CHAR_CLIENT_CONFIG;
|
||||
esp_ble_gatts_add_char_descr(gl_profile_tab[PROFILE_A_APP_ID].service_handle, &gl_profile_tab[PROFILE_A_APP_ID].descr_uuid,
|
||||
ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
|
||||
break;
|
||||
case ESP_GATTS_ADD_CHAR_DESCR_EVT:
|
||||
LOG_INFO("ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
||||
param->add_char.status, param->add_char.attr_handle, param->add_char.service_handle);
|
||||
break;
|
||||
case ESP_GATTS_DELETE_EVT:
|
||||
break;
|
||||
case ESP_GATTS_START_EVT:
|
||||
LOG_INFO("SERVICE_START_EVT, status %d, service_handle %d\n",
|
||||
param->start.status, param->start.service_handle);
|
||||
break;
|
||||
case ESP_GATTS_STOP_EVT:
|
||||
break;
|
||||
case ESP_GATTS_CONNECT_EVT:
|
||||
LOG_INFO("SERVICE_START_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
|
||||
param->connect.conn_id,
|
||||
param->connect.remote_bda[0], param->connect.remote_bda[1], param->connect.remote_bda[2],
|
||||
param->connect.remote_bda[3], param->connect.remote_bda[4], param->connect.remote_bda[5],
|
||||
param->connect.is_connected);
|
||||
gl_profile_tab[PROFILE_A_APP_ID].conn_id = param->connect.conn_id;
|
||||
break;
|
||||
case ESP_GATTS_DISCONNECT_EVT:
|
||||
case ESP_GATTS_OPEN_EVT:
|
||||
case ESP_GATTS_CANCEL_OPEN_EVT:
|
||||
case ESP_GATTS_CLOSE_EVT:
|
||||
case ESP_GATTS_LISTEN_EVT:
|
||||
case ESP_GATTS_CONGEST_EVT:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
|
||||
switch (event) {
|
||||
case ESP_GATTS_REG_EVT:
|
||||
LOG_INFO("REGISTER_APP_EVT, status %d, app_id %d\n", param->reg.status, param->reg.app_id);
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.is_primary = true;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.inst_id = 0x00;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.len = ESP_UUID_LEN_16;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.uuid.uuid16 = GATTS_SERVICE_UUID_TEST_B;
|
||||
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gap_config_adv_data(&test_adv_data);
|
||||
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_A_APP_ID].service_id, GATTS_NUM_HANDLE_TEST_B);
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
break;
|
||||
case ESP_GATTS_READ_EVT: {
|
||||
LOG_INFO("GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
|
||||
esp_gatt_rsp_t rsp;
|
||||
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
|
||||
rsp.attr_value.handle = param->read.handle;
|
||||
rsp.attr_value.len = 4;
|
||||
rsp.attr_value.value[0] = 0xde;
|
||||
rsp.attr_value.value[1] = 0xed;
|
||||
rsp.attr_value.value[2] = 0xbe;
|
||||
rsp.attr_value.value[3] = 0xef;
|
||||
esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id,
|
||||
ESP_GATT_OK, &rsp);
|
||||
break;
|
||||
}
|
||||
case ESP_GATTS_WRITE_EVT: {
|
||||
LOG_INFO("GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
|
||||
LOG_INFO("GATT_WRITE_EVT, value len %d, value %08x\n", param->write.len, *(uint32_t *)param->write.value);
|
||||
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
|
||||
break;
|
||||
}
|
||||
case ESP_GATTS_EXEC_WRITE_EVT:
|
||||
case ESP_GATTS_MTU_EVT:
|
||||
case ESP_GATTS_CONF_EVT:
|
||||
case ESP_GATTS_UNREG_EVT:
|
||||
break;
|
||||
case ESP_GATTS_CREATE_EVT:
|
||||
LOG_INFO("CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle);
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_handle = param->create.service_handle;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.len = ESP_UUID_LEN_16;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST_B;
|
||||
|
||||
esp_ble_gatts_start_service(gl_profile_tab[PROFILE_A_APP_ID].service_handle);
|
||||
|
||||
@ -270,6 +373,7 @@ void app_main()
|
||||
esp_ble_gatts_register_callback(gatts_event_handler);
|
||||
esp_ble_gap_register_callback(gap_event_handler);
|
||||
esp_ble_gatts_app_register(PROFILE_A_APP_ID);
|
||||
esp_ble_gatts_app_register(PROFILE_B_APP_ID);
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -41,13 +41,32 @@
|
||||
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
|
||||
static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
|
||||
static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
|
||||
static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
|
||||
|
||||
|
||||
static esp_gatt_srvc_id_t alert_service_id = {
|
||||
.id = {
|
||||
.uuid = {
|
||||
.len = ESP_UUID_LEN_16,
|
||||
.uuid = {.uuid16 = 0x1811,},
|
||||
},
|
||||
.inst_id = 0,
|
||||
},
|
||||
.is_primary = true,
|
||||
};
|
||||
|
||||
static esp_gatt_id_t notify_descr_id = {
|
||||
.uuid = {
|
||||
.len = ESP_UUID_LEN_16,
|
||||
.uuid = {.uuid16 = GATT_UUID_CHAR_CLIENT_CONFIG,},
|
||||
},
|
||||
.inst_id = 0,
|
||||
};
|
||||
#define BT_BD_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
|
||||
#define BT_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
|
||||
|
||||
static bool connect = false;
|
||||
static const char device_name[] = "Heart Rate";
|
||||
static const char device_name[] = "Alert Notification";
|
||||
|
||||
static esp_ble_scan_params_t ble_scan_params = {
|
||||
.scan_type = BLE_SCAN_TYPE_ACTIVE,
|
||||
@ -67,6 +86,7 @@ struct gattc_profile_inst {
|
||||
uint16_t gattc_if;
|
||||
uint16_t app_id;
|
||||
uint16_t conn_id;
|
||||
esp_bd_addr_t remote_bda;
|
||||
};
|
||||
|
||||
/* One gatt-based profile one app_id and one gattc_if, this array will store the gattc_if returned by ESP_GATTS_REG_EVT */
|
||||
@ -76,7 +96,7 @@ static struct gattc_profile_inst gl_profile_tab[PROFILE_NUM] = {
|
||||
.gattc_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
|
||||
},
|
||||
[PROFILE_B_APP_ID] = {
|
||||
.gattc_cb = NULL, /* This demo does not implement, similar as profile A */
|
||||
.gattc_cb = gattc_profile_b_event_handler,
|
||||
.gattc_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
|
||||
},
|
||||
};
|
||||
@ -93,12 +113,21 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
break;
|
||||
case ESP_GATTC_OPEN_EVT:
|
||||
conn_id = p_data->open.conn_id;
|
||||
LOG_INFO("ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d\n", conn_id, gattc_if, p_data->open.status);
|
||||
|
||||
memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
|
||||
LOG_INFO("ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d\n", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
|
||||
|
||||
LOG_INFO("REMOTE BDA %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
gl_profile_tab[PROFILE_A_APP_ID].remote_bda[0], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[1],
|
||||
gl_profile_tab[PROFILE_A_APP_ID].remote_bda[2], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[3],
|
||||
gl_profile_tab[PROFILE_A_APP_ID].remote_bda[4], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[5]
|
||||
);
|
||||
|
||||
esp_ble_gattc_search_service(gattc_if, conn_id, NULL);
|
||||
break;
|
||||
case ESP_GATTC_SEARCH_RES_EVT: {
|
||||
esp_gatt_srvc_id_t *srvc_id = &p_data->search_res.srvc_id;
|
||||
conn_id = p_data->open.conn_id;
|
||||
conn_id = p_data->search_res.conn_id;
|
||||
LOG_INFO("SEARCH RES: conn_id = %x\n", conn_id);
|
||||
if (srvc_id->id.uuid.len == ESP_UUID_LEN_16) {
|
||||
LOG_INFO("UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
|
||||
@ -119,6 +148,135 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
case ESP_GATTC_SEARCH_CMPL_EVT:
|
||||
conn_id = p_data->search_cmpl.conn_id;
|
||||
LOG_INFO("SEARCH_CMPL: conn_id = %x, status %d\n", conn_id, p_data->search_cmpl.status);
|
||||
esp_ble_gattc_get_characteristic(gattc_if, conn_id, &alert_service_id, NULL);
|
||||
break;
|
||||
case ESP_GATTC_GET_CHAR_EVT:
|
||||
if (p_data->get_char.status != ESP_GATT_OK) {
|
||||
break;
|
||||
}
|
||||
LOG_INFO("GET CHAR: conn_id = %x, status %d\n", p_data->get_char.conn_id, p_data->get_char.status);
|
||||
LOG_INFO("GET CHAR: srvc_id = %04x, char_id = %04x\n", p_data->get_char.srvc_id.id.uuid.uuid.uuid16, p_data->get_char.char_id.uuid.uuid.uuid16);
|
||||
|
||||
if (p_data->get_char.char_id.uuid.uuid.uuid16 == 0x2a46) {
|
||||
LOG_INFO("register notify\n");
|
||||
esp_ble_gattc_register_for_notify(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, &alert_service_id, &p_data->get_char.char_id);
|
||||
}
|
||||
|
||||
esp_ble_gattc_get_characteristic(gattc_if, conn_id, &alert_service_id, &p_data->get_char.char_id);
|
||||
break;
|
||||
case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
|
||||
uint16_t notify_en = 1;
|
||||
LOG_INFO("REG FOR NOTIFY: status %d\n", p_data->reg_for_notify.status);
|
||||
LOG_INFO("REG FOR_NOTIFY: srvc_id = %04x, char_id = %04x\n", p_data->reg_for_notify.srvc_id.id.uuid.uuid.uuid16, p_data->reg_for_notify.char_id.uuid.uuid.uuid16);
|
||||
|
||||
esp_ble_gattc_write_char_descr(
|
||||
gattc_if,
|
||||
conn_id,
|
||||
&alert_service_id,
|
||||
&p_data->reg_for_notify.char_id,
|
||||
¬ify_descr_id,
|
||||
sizeof(notify_en),
|
||||
(uint8_t *)¬ify_en,
|
||||
ESP_GATT_WRITE_TYPE_RSP,
|
||||
ESP_GATT_AUTH_REQ_NONE);
|
||||
break;
|
||||
}
|
||||
case ESP_GATTC_NOTIFY_EVT:
|
||||
LOG_INFO("NOTIFY: len %d, value %08x\n", p_data->notify.value_len, *(uint32_t *)p_data->notify.value);
|
||||
break;
|
||||
case ESP_GATTC_WRITE_DESCR_EVT:
|
||||
LOG_INFO("WRITE: status %d\n", p_data->write.status);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
|
||||
{
|
||||
uint16_t conn_id = 0;
|
||||
esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param;
|
||||
|
||||
switch (event) {
|
||||
case ESP_GATTC_REG_EVT:
|
||||
LOG_INFO("REG_EVT\n");
|
||||
//esp_ble_gap_set_scan_params(&ble_scan_params);
|
||||
break;
|
||||
case ESP_GATTC_OPEN_EVT:
|
||||
conn_id = p_data->open.conn_id;
|
||||
|
||||
memcpy(gl_profile_tab[PROFILE_B_APP_ID].remote_bda, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
|
||||
LOG_INFO("ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d\n", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
|
||||
|
||||
LOG_INFO("REMOTE BDA %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
gl_profile_tab[PROFILE_B_APP_ID].remote_bda[0], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[1],
|
||||
gl_profile_tab[PROFILE_B_APP_ID].remote_bda[2], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[3],
|
||||
gl_profile_tab[PROFILE_B_APP_ID].remote_bda[4], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[5]
|
||||
);
|
||||
|
||||
esp_ble_gattc_search_service(gattc_if, conn_id, NULL);
|
||||
break;
|
||||
case ESP_GATTC_SEARCH_RES_EVT: {
|
||||
esp_gatt_srvc_id_t *srvc_id = &p_data->search_res.srvc_id;
|
||||
conn_id = p_data->search_res.conn_id;
|
||||
LOG_INFO("SEARCH RES: conn_id = %x\n", conn_id);
|
||||
if (srvc_id->id.uuid.len == ESP_UUID_LEN_16) {
|
||||
LOG_INFO("UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
|
||||
LOG_INFO("UUID32: %x\n", srvc_id->id.uuid.uuid.uuid32);
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_128) {
|
||||
LOG_INFO("UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\n", srvc_id->id.uuid.uuid.uuid128[0],
|
||||
srvc_id->id.uuid.uuid.uuid128[1], srvc_id->id.uuid.uuid.uuid128[2], srvc_id->id.uuid.uuid.uuid128[3],
|
||||
srvc_id->id.uuid.uuid.uuid128[4], srvc_id->id.uuid.uuid.uuid128[5], srvc_id->id.uuid.uuid.uuid128[6],
|
||||
srvc_id->id.uuid.uuid.uuid128[7], srvc_id->id.uuid.uuid.uuid128[8], srvc_id->id.uuid.uuid.uuid128[9],
|
||||
srvc_id->id.uuid.uuid.uuid128[10], srvc_id->id.uuid.uuid.uuid128[11], srvc_id->id.uuid.uuid.uuid128[12],
|
||||
srvc_id->id.uuid.uuid.uuid128[13], srvc_id->id.uuid.uuid.uuid128[14], srvc_id->id.uuid.uuid.uuid128[15]);
|
||||
} else {
|
||||
LOG_ERROR("UNKNOWN LEN %d\n", srvc_id->id.uuid.len);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ESP_GATTC_SEARCH_CMPL_EVT:
|
||||
conn_id = p_data->search_cmpl.conn_id;
|
||||
LOG_INFO("SEARCH_CMPL: conn_id = %x, status %d\n", conn_id, p_data->search_cmpl.status);
|
||||
esp_ble_gattc_get_characteristic(gattc_if, conn_id, &alert_service_id, NULL);
|
||||
break;
|
||||
case ESP_GATTC_GET_CHAR_EVT:
|
||||
if (p_data->get_char.status != ESP_GATT_OK) {
|
||||
break;
|
||||
}
|
||||
LOG_INFO("GET CHAR: conn_id = %x, status %d\n", p_data->get_char.conn_id, p_data->get_char.status);
|
||||
LOG_INFO("GET CHAR: srvc_id = %04x, char_id = %04x\n", p_data->get_char.srvc_id.id.uuid.uuid.uuid16, p_data->get_char.char_id.uuid.uuid.uuid16);
|
||||
|
||||
if (p_data->get_char.char_id.uuid.uuid.uuid16 == 0x2a46) {
|
||||
LOG_INFO("register notify\n");
|
||||
esp_ble_gattc_register_for_notify(gattc_if, gl_profile_tab[PROFILE_B_APP_ID].remote_bda, &alert_service_id, &p_data->get_char.char_id);
|
||||
}
|
||||
|
||||
esp_ble_gattc_get_characteristic(gattc_if, conn_id, &alert_service_id, &p_data->get_char.char_id);
|
||||
break;
|
||||
case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
|
||||
uint16_t notify_en = 1;
|
||||
LOG_INFO("REG FOR NOTIFY: status %d\n", p_data->reg_for_notify.status);
|
||||
LOG_INFO("REG FOR_NOTIFY: srvc_id = %04x, char_id = %04x\n", p_data->reg_for_notify.srvc_id.id.uuid.uuid.uuid16, p_data->reg_for_notify.char_id.uuid.uuid.uuid16);
|
||||
|
||||
esp_ble_gattc_write_char_descr(
|
||||
gattc_if,
|
||||
conn_id,
|
||||
&alert_service_id,
|
||||
&p_data->reg_for_notify.char_id,
|
||||
¬ify_descr_id,
|
||||
sizeof(notify_en),
|
||||
(uint8_t *)¬ify_en,
|
||||
ESP_GATT_WRITE_TYPE_RSP,
|
||||
ESP_GATT_AUTH_REQ_NONE);
|
||||
break;
|
||||
}
|
||||
case ESP_GATTC_NOTIFY_EVT:
|
||||
LOG_INFO("NOTIFY: len %d, value %08x\n", p_data->notify.value_len, *(uint32_t *)p_data->notify.value);
|
||||
break;
|
||||
case ESP_GATTC_WRITE_DESCR_EVT:
|
||||
LOG_INFO("WRITE: status %d\n", p_data->write.status);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -164,6 +322,7 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par
|
||||
LOG_INFO("Connect to the remote device.\n");
|
||||
esp_ble_gap_stop_scanning();
|
||||
esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, true);
|
||||
esp_ble_gattc_open(gl_profile_tab[PROFILE_B_APP_ID].gattc_if, scan_result->scan_rst.bda, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -229,6 +388,7 @@ void ble_client_appRegister(void)
|
||||
return;
|
||||
}
|
||||
esp_ble_gattc_app_register(PROFILE_A_APP_ID);
|
||||
esp_ble_gattc_app_register(PROFILE_B_APP_ID);
|
||||
}
|
||||
|
||||
void gattc_client_test(void)
|
||||
|
Loading…
x
Reference in New Issue
Block a user