使用ble_gatt_server例程手机蓝牙可配对但是无法连接
Posted: Wed Mar 27, 2024 12:53 pm
我所用的设备是合宙的esp32c3,所用的编辑器为vscode,idf的版本应该是4.3-4.4左右。例程中我主要更改了passkey和输入方式,在配对时手动输入pin码进行配对,在进行重启手机,复位芯片,更换芯片之后,手机的蓝牙界面一直都无法进行连接,尽管已经配对上了,app_main的代码如下:
随后芯片从启动到配对蓝牙的日志如下,我看了一下应该是没有问题的,但点击手机的蓝牙选项卡就是没有连接的反应(小米10s)
Code: Select all
void app_main(void)
{
esp_err_t ret;
// Initialize NVS.
ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret) {
ESP_LOGE(GATTS_TABLE_TAG, "%s init controller failed: %s", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret) {
ESP_LOGE(GATTS_TABLE_TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(ret));
return;
}
ESP_LOGI(GATTS_TABLE_TAG, "%s init bluetooth", __func__);
ret = esp_bluedroid_init();
if (ret) {
ESP_LOGE(GATTS_TABLE_TAG, "%s init bluetooth failed: %s", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bluedroid_enable();
if (ret) {
ESP_LOGE(GATTS_TABLE_TAG, "%s enable bluetooth failed: %s", __func__, esp_err_to_name(ret));
return;
}
ret = esp_ble_gatts_register_callback(gatts_event_handler);
if (ret){
ESP_LOGE(GATTS_TABLE_TAG, "gatts register error, error code = %x", ret);
return;
}
ret = esp_ble_gap_register_callback(gap_event_handler);
if (ret){
ESP_LOGE(GATTS_TABLE_TAG, "gap register error, error code = %x", ret);
return;
}
ret = esp_ble_gatts_app_register(ESP_HEART_RATE_APP_ID);
if (ret){
ESP_LOGE(GATTS_TABLE_TAG, "gatts app register error, error code = %x", ret);
return;
}
/* set the security iocap & auth_req & key size & init key response key parameters to the stack*/
esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND; //bonding with peer device after authentication
esp_ble_io_cap_t iocap = ESP_IO_CAP_OUT; //set the IO capability to No output No input
uint8_t key_size = 16; //the key size should be 7~16 bytes
uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
//set static passkey
uint32_t passkey = 100101;
uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE;
uint8_t oob_support = ESP_BLE_OOB_DISABLE;
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t));
/* If your BLE device acts as a Slave, the init_key means you hope which types of key of the master should distribute to you,
and the response key means which key you can distribute to the master;
If your BLE device acts as a master, the response key means you hope which types of key of the slave should distribute to you,
and the init key means which key you can distribute to the slave. */
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));
/* Just show how to clear all the bonded devices
* Delay 30s, clear all the bonded devices
*
* vTaskDelay(30000 / portTICK_PERIOD_MS);
* remove_all_bonded_devices();
*/
}
已经尝试了许多方法未曾解决,希望能得到各位的帮助,谢谢!---- 已打开串行端口 COM4 ----
I (164) spi_flash: detected chip: generic
I (164) spi_flash: flash io: dio
I (165) sleep: Configure to isolate all GPIO pins in sleep state
I (168) sleep: Enable automatic switching of GPIO sleep configuration
I (175) cpu_start: Starting scheduler.
W (185) BTDM_INIT: esp_bt_controller_mem_release not implemented, return OK
I (185) BTDM_INIT: BT controller compile version [501d88d]
I (195) coexist: coexist rom version 9387209
I (195) phy_init: phy_version 500,985899c,Apr 19 2021,16:05:08
I (315) system_api: Base MAC address is not set
I (315) system_api: read default base MAC address from EFUSE
I (315) BTDM_INIT: Bluetooth MAC: 48:31:b7:03:af:25
I (325) SEC_GATTS_DEMO: app_main init bluetooth
I (345) SEC_GATTS_DEMO: The number handle = 8
I (355) SEC_GATTS_DEMO: advertising start success
I (44985) SEC_GATTS_DEMO: ESP_GATTS_CONNECT_EVT
I (45195) SEC_GATTS_DEMO: The passkey Notify number:100101
W (62375) BT_SMP: FOR LE SC LTK IS USED INSTEAD OF STK
I (62535) SEC_GATTS_DEMO: key type = ESP_LE_KEY_LENC
I (62535) SEC_GATTS_DEMO: key type = ESP_LE_KEY_PENC
I (62535) SEC_GATTS_DEMO: key type = ESP_LE_KEY_LID
I (62605) SEC_GATTS_DEMO: key type = ESP_LE_KEY_PID
I (62615) SEC_GATTS_DEMO: remote BD_ADDR: 4ecaae3300c3
I (62615) SEC_GATTS_DEMO: address type = 1
I (62615) SEC_GATTS_DEMO: pair status = success
I (62625) SEC_GATTS_DEMO: auth mode = ESP_LE_AUTH_REQ_SC_MITM_BOND
I (62635) SEC_GATTS_DEMO: Bonded devices number : 1
I (62635) SEC_GATTS_DEMO: Bonded devices list : 1
I (62645) SEC_GATTS_DEMO: 4e ca ae 33 00 c3