乐鑫给的例程中是按照蓝牙设备名称去连接子设备,修改为按照mac地址连接子设备
1.获取广播中的mac地址
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_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);
// for(int i=0;i<6;i++){
// printf("%02x",scan_result->scan_rst.bda[i]);
// // adv_mac[i]=scan_result->scan_rst.bda[i];
// printf(" ");
// // esp_log_buffer_hex(GATTC_TAG, scan_result->scan_rst.bda[i], 1);
// }
memcpy(adv_mac, scan_result->scan_rst.bda, sizeof(esp_bd_addr_t));
esp_log_buffer_hex(GATTC_TAG, adv_mac, 6);
if(memcmp(adv_mac, mac, 6) == 0){
ESP_LOGI(GATTC_TAG, "esp mac found");
}
2.通过mac发起连接
if (adv_name != NULL) {
if (strlen(remote_device_name) == adv_name_len && memcmp(adv_mac, mac, 6) == 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.");
// esp_log_buffer_hex(GATTC_TAG, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, sizeof(esp_bd_addr_t));
esp_log_buffer_hex(GATTC_TAG, adv_mac, sizeof(esp_bd_addr_t));
esp_ble_gap_stop_scanning();
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);
}
}
}