hi
用Nordic52840的时候 有一个NUS服务 留下深刻印象 在Nordic的代码里可以看到它的函数是如下这样
ble_nus_init
sd_ble_uuid_vs_add
sd_ble_gatts_service_add
characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->rx_handles)
characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->tx_handles);
它有一个NUS服务 还有ADD了两个char分别是RX TX
通过手机APP也很容易看明白
毕竟手机APP也是Nordic做的
现在用IDF的案例跑在ESP32上面 案例是一对儿
examples\bluetooth\bluedroid\ble\gatt_client\main\gattc_demo.c
examples\bluetooth\bluedroid\ble\gatt_server\main\gatts_demo.c
我想修改从机 也就是gatt_server\main\gatts_demo.c的代码 用ESP32伪造一个nordic的设备
通过修改UUID很容易 修改service
这样手机APP扫到它的时候 就识别NUS服务
但是增加char遇到麻烦
esp32代码看上去只有一个char 我不能把TX RX同时加上去
接口是这个
esp_ble_gatts_add_char
想问问 该怎么写代码呢?还是ESP的SDK无法做到这一点
下面是我修改的代码 测试是不行的
`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) {
uint8_t custs1_svc1[16]= {0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x01, 0x00, 0x40, 0x6E};
uint8_t adv_char_uuid128[16]= {0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x02, 0x00, 0x40, 0x6E};
uint8_t adv_char_uuid1282[16]= {0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x03, 0x00, 0x40, 0x6E};
switch (event) {
case ESP_GATTS_CREATE_EVT:
ESP_LOGI(GATTS_TAG, "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_128;
memcpy(gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid128,adv_char_uuid128,ESP_UUID_LEN_128);
esp_ble_gatts_start_service(gl_profile_tab[PROFILE_A_APP_ID].service_handle);
a_property = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
esp_err_t add_char_ret = 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,
a_property,
&gatts_demo_char1_val, NULL);
if (add_char_ret){
ESP_LOGE(GATTS_TAG, "add char failed, error code =%x",add_char_ret);
}
ESP_LOGI(GATTS_TAG, "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_128;
memcpy(gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid128,adv_char_uuid1282,ESP_UUID_LEN_128);
esp_ble_gatts_start_service(gl_profile_tab[PROFILE_A_APP_ID].service_handle);
a_property = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
esp_err_t add_char_ret2 = 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,
a_property,
&gatts_demo_char1_val, NULL);
if (add_char_ret2){
ESP_LOGE(GATTS_TAG, "add char failed, error code =%x",add_char_ret2);
}
break;`