example: bluetooth/bluedroid/ble/gatt_client
我的修改:
在esp_gap_cb中ESP_GAP_BLE_SCAN_START_COMPLETE_EVT事件 添加了resp adv中解析蓝牙设备名称的代码,其他无改动
Code: Select all
case ESP_GAP_BLE_SCAN_RESULT_EVT: {
esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param;
switch (scan_result->scan_rst.search_evt) {
case ESP_GAP_SEARCH_INQ_RES_EVT:
ESP_LOGI(GATTC_TAG, "----------Device Found-----mac addr-----");
esp_log_buffer_hex(GATTC_TAG, scan_result->scan_rst.bda, 6);
ESP_LOGI(GATTC_TAG, "searched Adv Data Len %d, Scan Response Len %d", scan_result->scan_rst.adv_data_len, scan_result->scan_rst.scan_rsp_len);
adv_name = esp_ble_resolve_adv_data(scan_result->scan_rst.ble_adv,
ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
ESP_LOGI(GATTC_TAG, "searched Device Name Len %d", adv_name_len);
ESP_LOGI(GATTC_TAG, "ble name:");
esp_log_buffer_char(GATTC_TAG, adv_name, adv_name_len);
#if CONFIG_EXAMPLE_DUMP_ADV_DATA_AND_SCAN_RESP
if (scan_result->scan_rst.adv_data_len > 0) {
ESP_LOGI(GATTC_TAG, "adv data:");
esp_log_buffer_hex(GATTC_TAG, &scan_result->scan_rst.ble_adv[0], scan_result->scan_rst.adv_data_len);
}
if (scan_result->scan_rst.scan_rsp_len > 0) {
ESP_LOGI(GATTC_TAG, "scan resp:");
esp_log_buffer_hex(GATTC_TAG, &scan_result->scan_rst.ble_adv[scan_result->scan_rst.adv_data_len], scan_result->scan_rst.scan_rsp_len);
/* add by aaa*/
adv_name = esp_ble_resolve_adv_data(&scan_result->scan_rst.ble_adv[scan_result->scan_rst.adv_data_len],
ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
ESP_LOGI(GATTC_TAG, "11searched Device Name Len %d", adv_name_len);
ESP_LOGI(GATTC_TAG, "11ble name:");
esp_log_buffer_char(GATTC_TAG, adv_name, adv_name_len);
/* end add by aaa*/
}
#endif
ESP_LOGI(GATTC_TAG, "\n");
if (adv_name != NULL) {
if (strlen(remote_device_name) == adv_name_len && strncmp((char *)adv_name, remote_device_name, adv_name_len) == 0) {
ESP_LOGI(GATTC_TAG, "searched device %s\n", remote_device_name);
if (connect == false) {
connect = true;
ESP_LOGI(GATTC_TAG, "connect to the remote device.1\n");
uint8_t ret = esp_ble_gap_stop_scanning();
ESP_LOGI(GATTC_TAG, "esp_ble_gap_stop_scanning ret = %d\n",ret);
//esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, scan_result->scan_rst.ble_addr_type, true);
}
}
}
ESP_LOGI(GATTC_TAG, "vz end ESP_GAP_BLE_SCAN_RESULT_EVT\n");
break;