ESP32的GATTC demo可以与GATTS demo实现互相通信,UUID的配置也十分简单。
以下是demo中的UUID配置。
注意:16bit和32bit的UUID只需要设置UUID的前16bit或者32bit,剩余的bit使用的是Bluetooth_Base_UUID:xxxxxxxx-0000-1000-8000-00805F9B34FB
#define REMOTE_SERVICE_UUID 0x00FF
#define REMOTE_NOTIFY_CHAR_UUID 0xFF01
......
//Server UUID
static esp_bt_uuid_t remote_filter_service_uuid = {
.len = ESP_UUID_LEN_16,
.uuid = {.uuid16 = REMOTE_SERVICE_UUID,},
};
//notify UUID
static esp_bt_uuid_t remote_filter_char_uuid = {
.len = ESP_UUID_LEN_16,
.uuid = {.uuid16 = REMOTE_NOTIFY_CHAR_UUID,},
};
//Characteristic UUID
static esp_bt_uuid_t notify_descr_uuid = {
.len = ESP_UUID_LEN_16,
.uuid = {.uuid16 = ESP_GATT_UUID_CHAR_CLIENT_CONFIG,},
};
当GATTS所使用的UUID不是Bluetooth_Base_UUID的时候,需要把Server的UUID完整写入。此时,就不能按demo的写法进行修改,因为数据过长,需要使用数组配置UUID。
注意:UUID的数组需要低位写在前,高位写在后。也就是说要倒过来写
typedef struct {
#define ESP_UUID_LEN_16 2
#define ESP_UUID_LEN_32 4
#define ESP_UUID_LEN_128 16
uint16_t len; /*!< UUID length, 16bit, 32bit or 128bit */
union {
uint16_t uuid16;
uint32_t uuid32;
uint8_t uuid128[ESP_UUID_LEN_128];
} uuid; /*!< UUID */
} __attribute__((packed)) esp_bt_uuid_t;
.................................................................
//Server UUID
static esp_bt_uuid_t remote_filter_service_uuid = {
.len = ESP_UUID_LEN_128,
/* LSB <--------------------------------------------------------------------------------> MSB */
.uuid = {.uuid128 = {0x9e,0xca,0xdc,0x24,0x0e,0xe5,0xa9,0xe0,0x93,0xf3,0xa3,0xb5,0x01,0x00,0x40,0x6e},},
};
//notify UUID
static esp_bt_uuid_t remote_filter_char_uuid = {
.len = ESP_UUID_LEN_128,
/* LSB <--------------------------------------------------------------------------------> MSB */
.uuid = {.uuid128 = {0x9e,0xca,0xdc,0x24,0x0e,0xe5,0xa9,0xe0,0x93,0xf3,0xa3,0xb5,0x03,0x00,0x40,0x6e},},
};
//Characteristic UUID
static esp_bt_uuid_t notify_descr_uuid = {
.len = ESP_UUID_LEN_16,
.uuid = {.uuid16 = ESP_GATT_UUID_CHAR_CLIENT_CONFIG,},
};
如果不清楚UUID的格式,可以使用nrf connect等手机APP。
也可以通过demo中的回调函数输出目标蓝牙的所有server uuid。(demo通过蓝牙名称进行连接)
1、首先在discover service complete回调中修改server搜索函数,把按server uuid搜索改成NULL,搜索所有uuid。
case ESP_GATTC_DIS_SRVC_CMPL_EVT:
if (param->dis_srvc_cmpl.status != ESP_GATT_OK) {
ESP_LOGE(COEX_TAG, "discover service failed, status %d\n", param->dis_srvc_cmpl.status);
break;
}
ESP_LOGI(COEX_TAG, "discover service complete conn_id %d\n", param->dis_srvc_cmpl.conn_id);
//esp_ble_gattc_search_service(gattc_if, param->cfg_mtu.conn_id, &remote_filter_service_uuid);
esp_ble_gattc_search_service(gattc_if, param->cfg_mtu.conn_id, NULL);
break;
2、然后在service discovery result is got的回调中输出所有服务的uuid长度以及具体数据。
case ESP_GATTC_SEARCH_RES_EVT:
ESP_LOGI(COEX_TAG, "SEARCH RES: conn_id = %x is primary service %d\n", p_data->search_res.conn_id, p_data->search_res.is_primary);
ESP_LOGI(COEX_TAG, "start handle %d end handle %d current handle value %d\n", p_data->search_res.start_handle, p_data->search_res.end_handle, p_data->search_res.srvc_id.inst_id);
printf("--------------------------\r\n");
for(int i=0;i<16;i++){
printf("%02x",p_data->search_res.srvc_id.uuid.uuid.uuid128[i]);
}
printf("uuid_len = %d\r\n",p_data->search_res.srvc_id.uuid.len);
printf("--------------------------\r\n");
if (p_data->search_res.srvc_id.uuid.len == ESP_UUID_LEN_128 && memcmp(p_data->search_res.srvc_id.uuid.uuid.uuid128,remote_uuid,16) == 0/*p_data->search_res.srvc_id.uuid.uuid.uuid16 == REMOTE_SERVICE_UUID*/) {
ESP_LOGI(COEX_TAG, "service found\n");
get_server = true;
gattc_profile_tab[GATTC_PROFILE_C_APP_ID].service_start_handle = p_data->search_res.start_handle;
gattc_profile_tab[GATTC_PROFILE_C_APP_ID].service_end_handle = p_data->search_res.end_handle;
ESP_LOGI(COEX_TAG, "UUID16: %s\n", p_data->search_res.srvc_id.uuid.uuid.uuid128);
}
break;
3、最后按得到的UUID配置自己的UUID即可。