diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp b/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp index 25dba4e2f2..401bf554c5 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp @@ -646,7 +646,15 @@ ble_error_t Gap::rejectConnectionParametersUpdate( #if BLE_ROLE_CENTRAL ble_error_t Gap::cancelConnect() { - return _pal_gap.cancel_connection_creation(); + if (!_initiating) { + return BLE_ERROR_NONE; + } + + auto ret = _pal_gap.cancel_connection_creation(); + if (ret) { + _initiating = false; + } + return ret; } #endif @@ -1192,6 +1200,10 @@ void Gap::on_advertising_report(const GapAdvertisingReportEvent &e) void Gap::on_connection_complete(const GapConnectionCompleteEvent &e) { + if (e.role == connection_role_t::CENTRAL) { + _initiating = false; + } + if (e.status != hci_error_code_t::SUCCESS) { if (_event_handler) { _event_handler->onConnectionComplete( @@ -2332,6 +2344,10 @@ void Gap::on_enhanced_connection_complete( return; } + if (own_role == connection_role_t::CENTRAL) { + _initiating = false; + } + _event_handler->onConnectionComplete( ConnectionCompleteEvent( (status == hci_error_code_t::SUCCESS) ? BLE_ERROR_NONE : BLE_ERROR_INTERNAL_STACK_FAILURE, diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.h b/connectivity/FEATURE_BLE/source/generic/GapImpl.h index 40b9be6752..0d9ccab86b 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.h +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.h @@ -586,6 +586,7 @@ private: mbed::LowPowerTimeout _scan_timeout; mbed::LowPowerTicker _address_rotation_ticker; + bool _initiating = false; template struct BitArray { BitArray() : data()