Skip to content

When async-client fails to connect, the client callback onConnectFail doesn't get called #1006

@tbekas

Description

@tbekas

NimBLE-Arduino: 2.3.2
Platform: pioarduino 54.03.20
Framework: Arduino 3.2.0
ESP-IDF: v5.4.1
Board: ESP32-C6
BLE Device: Xbox Wireless Controller (firmware 5.x)

On about ~10-20% connection attempts, my BLE gamepad doesn't seem to connect successfully with the board. It wouldn't be a problem if I my onConnectFail callback would get called, but it doesn't.

This is the log from unsuccessfull connect attempt:

I NimBLEScan: New advertiser: ec:83:50:6e:5c:71
D NimBLEScanCallbacks: Discovered: Name: , Address: ec:83:50:6e:5c:71, appearance: 964, manufacturer data: 060000, serviceUUID: 0x1812, txPower: 20
I NimBLEScan: Scan response from: ec:83:50:6e:5c:71
DEBUG Device discovered, address: ec:83:50:6e:5c:71, address type: 0, name: Xbox Wireless Controller
INFO Attempting to connect to a device, address: ec:83:50:6e:5c:71
D NimBLEClient: >> connect(ec:83:50:6e:5c:71)
D NimBLEScan: >> stop()
D NimBLEScan: << stop()
D NimBLEScan: erase device: ec:83:50:6e:5c:71
D NimBLEClient: >> handleGapEvent BLE_GAP_EVENT_DISCONNECT
D NimBLEClient: my inserted log: disconnect reason is 0x023e
D NimBLEClient: my inserted log: event->disconnect.conn.conn_handle is 0

no more logs

I was digging into the library code so I've inserted two log lines into the NimBLEClient (inserted just after the line 935). The logged reason is Connection Failed to be Established according to docs. While the conn_handle from event object is 0.

switch (event->type) {
case BLE_GAP_EVENT_DISCONNECT: {
// workaround for bug in NimBLE stack where disconnect event argument is not passed correctly
pClient = NimBLEDevice::getClientByHandle(event->disconnect.conn.conn_handle);
if (pClient == nullptr) {
return 0;
}

What happens next is that NimBLEDevice::getClientByHandle returns nullptr, so the function NimBLE::handleGapEvent simply returns 0 without notyfing the applicaiton code about an unsuccessful connection attempt.

My application code fragments:

For context - this is how logs look like on a successful connection attempt (80-90% of the time).

I NimBLEScan: New advertiser: ec:83:50:6e:5c:71
D NimBLEScanCallbacks: Discovered: Name: , Address: ec:83:50:6e:5c:71, appearance: 964, manufacturer data: 060000, serviceUUID: 0x1812, txPower: 20
I NimBLEScan: Scan response from: ec:83:50:6e:5c:71
DEBUG Device discovered, address: ec:83:50:6e:5c:71, address type: 0, name: Xbox Wireless Controller
INFO Attempting to connect to a device, address: ec:83:50:6e:5c:71
D NimBLEClient: >> connect(ec:83:50:6e:5c:71)
D NimBLEScan: >> stop()
D NimBLEScan: << stop()
D NimBLEScan: erase device: ec:83:50:6e:5c:71
D NimBLEClient: >> handleGapEvent BLE_GAP_EVENT_CONNECT
DEBUG Connected to a device, address: ec:83:50:6e:5c:71
D NimBLEClient: >> secureConnection()
D NimBLEClient: >> handleGapEvent BLE_GAP_EVENT_LINK_ESTAB
D NimBLEClient: >> handleGapEvent BLE_GAP_EVENT_DATA_LEN_CHG
D NimBLEClient: >> handleGapEvent BLE_GAP_EVENT_MTU
I NimBLEClient: mtu update: mtu=23
D NimBLEClientCallbacks: onMTUChange: default
D NimBLEClient: << handleGapEvent
D NimBLEClient: exchangeMTUCb: status=0, mtu=23
D NimBLEClient: >> handleGapEvent BLE_GAP_EVENT_PARING_COMPLETE
D NimBLEClient: >> handleGapEvent BLE_GAP_EVENT_ENC_CHANGE
INFO Bonded successfully with a device, address: ec:83:50:6e:5c:71

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions