NimBLE-Arduino icon indicating copy to clipboard operation
NimBLE-Arduino copied to clipboard

Unable to connect again after an out of range

Open morgan-wild opened this issue 3 months ago • 2 comments

I'm trying to connect 2 ESP32 with NimBle and the Arduino Framework.

I use the latest version of NimBLE-Arduino 2.3.6

Globally it works very well, excepted one thing at the client side I described in this use case:

  • My client device finds the gatt server and connects without any problem.
  • If I turn off my server, the onDisconnect event is fired (normal) and my client restarts a scan.
  • If I turn on my server now, the client detects it and connect again (still normal).
  • If I move one of these devices out of the range, the onDisconnect event is fired (still normal).
  • But if I move back the device in the range, the client detects the server, try to connect but stay stucked.

There is no onConnect, onConnectFail or onDisconnect event fired.

If I don't touch the server and restart the client, everything is good again.

I used client->connect as the async way.

    _pClient->setClientCallbacks(new BluetoothClientCallbacks(this));
    _pClient->setConnectTimeout(5000);
    
    if (!_pClient->connect(true, true, false)) {

        NimBLEDevice::deleteClient(_pClient);

        Serial.println("Failed to start async connect");

        return false;
    }

    Serial.println("Connecting...");

There is something broken or smething special to know in this case?

16:37:42.018 > onScanDiscovered <- CLIENT STARTS
16:37:42.034 > onScanResult
16:37:42.034 > onScanDiscovered
16:37:42.051 > onScanResult
16:37:42.084 > onScanDiscovered
16:37:42.100 > onScanResult
16:37:42.100 > Advertised Device found:Name: GroundModule, Address: 30:c6:f7:c2:f5:3a, serviceUUID: 77777777-7777-7777-0006-000000000000
16:37:42.166 > Connecting...
16:37:42.461 > onConnect
16:37:44.118 > Discovered 3 services <- CLIENT CONNECTED AND LISTENING NOTIFICATIONS
16:37:44.410 > #000000 <-RECEVING SOME NOTIFICATIONS FROM A CHARACTERISTIC
16:37:52.710 > #FFFFFF
16:37:53.561 > #000000
16:37:54.660 > #000000
16:37:55.760 > #FFFFFF
16:37:56.610 > #000000
16:37:57.611 > #000000
16:38:46.374 > onDisconnected <- OUT OF RANGE STARTS HERE
16:38:46.400 > onScanDiscovered
16:38:46.400 > onScanResult
16:38:46.417 > onScanDiscovered
16:38:48.919 > onScanResult
16:38:49.410 > onScanDiscovered
16:38:49.919 > onScanDiscovered
16:38:49.919 > onScanResult
16:38:50.411 > onScanResult  <- COMING BACK HERE
16:38:50.411 > Advertised Device found:Name: GroundModule, Address: 30:c6:f7:c2:f5:3a, serviceUUID: 77777777-7777-7777-0006-000000000000
16:38:50.475 > Connecting...  <-STUCKED

```

morgan-wild avatar Sep 17 '25 15:09 morgan-wild

The workaround I use to fix it:

    _pClient->setClientCallbacks(new BluetoothClientCallbacks(this));
    _pClient->setConnectTimeout(5000);
    
    if (!_pClient->connect(true, true, false)) {

        NimBLEDevice::deleteClient(_pClient);

        Serial.println("Failed to start async connect");

        return false;
    }

    _connectStartTime = millis(); <- HERE

    Serial.println("Connecting...");

void BluetoothClient::process() { <- CALLED PERIODICALLY

    if (_connectStartTime > 0 && millis() - _connectStartTime > 5000) {
    
        Serial.println("onConnectTimeout");

        _connectStartTime = 0;

        NimBLEDevice::deleteClient(_pClient);

        NimBLEDevice::getScan()->start(5000);
    }
}

morgan-wild avatar Sep 17 '25 15:09 morgan-wild

Could you enable debug logs and share the output when this happens please?

h2zero avatar Sep 18 '25 20:09 h2zero