Add NimBLEClientCallbacks::onConnectFail callback

Adds a callback that is called when the connection attempt fail while connecting asynchronously.
This commit is contained in:
h2zero 2024-11-24 19:46:34 -07:00 committed by h2zero
parent 41a7aa8eb5
commit a4043e3f04
2 changed files with 26 additions and 15 deletions

View file

@ -977,28 +977,28 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
rc = event->connect.status;
if (rc == 0) {
pClient->m_connHandle = event->connect.conn_handle;
if (pClient->m_config.asyncConnect) {
pClient->m_pClientCallbacks->onConnect(pClient);
}
if (pClient->m_config.exchangeMTU) {
if (!pClient->exchangeMTU() && !pClient->m_config.asyncConnect) {
if (!pClient->exchangeMTU()) {
rc = pClient->m_lastErr; // sets the error in the task data
break;
}
}
} else {
pClient->m_connHandle = BLE_HS_CONN_HANDLE_NONE;
if (!pClient->m_config.asyncConnect) {
break;
}
if (pClient->m_config.deleteOnConnectFail) { // async connect
delete pClient;
return 0;
}
}
if (pClient->m_config.asyncConnect) {
pClient->m_pClientCallbacks->onConnect(pClient);
} else if (!pClient->m_config.exchangeMTU) {
break; // not waiting for MTU exchange so release the task now.
pClient->m_pClientCallbacks->onConnectFail(pClient, rc);
if (pClient->m_config.deleteOnConnectFail) {
NimBLEDevice::deleteClient(pClient);
}
}
break;
}
return 0;
@ -1236,8 +1236,12 @@ void NimBLEClientCallbacks::onConnect(NimBLEClient* pClient) {
NIMBLE_LOGD(CB_TAG, "onConnect: default");
}
void NimBLEClientCallbacks::onConnectFail(NimBLEClient* pClient, int reason) {
NIMBLE_LOGD(CB_TAG, "onConnectFail: default, reason: %d", reason);
}
void NimBLEClientCallbacks::onDisconnect(NimBLEClient* pClient, int reason) {
NIMBLE_LOGD(CB_TAG, "onDisconnect: default");
NIMBLE_LOGD(CB_TAG, "onDisconnect: default, reason: %d", reason);
}
bool NimBLEClientCallbacks::onConnParamsUpdateRequest(NimBLEClient* pClient, const ble_gap_upd_params* params) {

View file

@ -148,10 +148,17 @@ class NimBLEClientCallbacks {
/**
* @brief Called after client connects.
* @param [in] pClient A pointer to the calling client object.
* @param [in] pClient A pointer to the connecting client object.
*/
virtual void onConnect(NimBLEClient* pClient);
/**
* @brief Called when a connection attempt fails.
* @param [in] pClient A pointer to the connecting client object.
* @param [in] reason Contains the reason code for the connection failure.
*/
virtual void onConnectFail(NimBLEClient* pClient, int reason);
/**
* @brief Called when disconnected from the server.
* @param [in] pClient A pointer to the calling client object.