@@ -238,15 +238,20 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
238238 */
239239bool NimBLEClient::secureConnection () {
240240 ble_task_data_t taskData = {this , xTaskGetCurrentTaskHandle (), 0 , nullptr };
241- m_pTaskData = &taskData;
242241
243- int rc = NimBLEDevice::startSecurity (m_conn_id);
244- if (rc != 0 ){
245- m_pTaskData = nullptr ;
246- return false ;
247- }
242+ int retryCount = 1 ;
248243
249- ulTaskNotifyTake (pdTRUE, portMAX_DELAY);
244+ do {
245+ m_pTaskData = &taskData;
246+
247+ int rc = NimBLEDevice::startSecurity (m_conn_id);
248+ if (rc != 0 ){
249+ m_pTaskData = nullptr ;
250+ return false ;
251+ }
252+
253+ ulTaskNotifyTake (pdTRUE, portMAX_DELAY);
254+ } while (taskData.rc == BLE_HS_HCI_ERR (BLE_ERR_PINKEY_MISSING) && retryCount--);
250255
251256 if (taskData.rc != 0 ){
252257 return false ;
@@ -822,12 +827,15 @@ uint16_t NimBLEClient::getMTU() {
822827 return 0 ;
823828 }
824829
825- if (event->enc_change .status == 0 ) {
830+ if (event->enc_change .status == 0 || event-> enc_change . status == BLE_HS_HCI_ERR (BLE_ERR_PINKEY_MISSING) ) {
826831 struct ble_gap_conn_desc desc;
827- rc = ble_gap_conn_find (event->conn_update .conn_handle , &desc);
832+ rc = ble_gap_conn_find (event->enc_change .conn_handle , &desc);
828833 assert (rc == 0 );
829834
830- if (NimBLEDevice::m_securityCallbacks != nullptr ) {
835+ if (event->enc_change .status == BLE_HS_HCI_ERR (BLE_ERR_PINKEY_MISSING)) {
836+ // Key is missing, try deleting.
837+ ble_store_util_delete_peer (&desc.peer_id_addr );
838+ } else if (NimBLEDevice::m_securityCallbacks != nullptr ) {
831839 NimBLEDevice::m_securityCallbacks->onAuthenticationComplete (&desc);
832840 } else {
833841 client->m_pClientCallbacks ->onAuthenticationComplete (&desc);
0 commit comments