Mattzobricks / MattzoControllers

MattzoController firmware
20 stars 10 forks source link

MTC4BT: unknown discovered devices should not count towards the BLE connection limit #35

Closed joosbuijsNL closed 1 year ago

joosbuijsNL commented 1 year ago

Unknown discovered devices count towards the BLE connection limit, 9 by default. This causes that known devices are unable to connect if too many unknown discovered devices are detected. Therefore unknown discovered devices should not count towards the maximum connection limit.

In the serial output below there are 3 known devices connected, 9 unknown devices, and a fourth known device which cannot connect anymore.


[0656] [0] BLE : Connecting to hub '90:84:2b:20:95:76'...
[0657] [0] BLE : Max clients reached - no more connections available.
[0658] [0] Loop: Connect failed. Will retry...
[0659] [0] BLE : Scanning for 1 hub(s)...
[0660] [0] BLE : Discovered hub:  (90:84:2b:20:95:76).
[0661] [0] BLE : Discovered unknown device:  (4b:74:15:5b:ee:bc).
[0662] [0] BLE : Discovered unknown device:  (56:e9:7b:07:6b:d5).
[0663] [0] BLE : Discovered unknown device:  (15:cc:91:53:95:e5).
[0664] [0] BLE : Discovered unknown device:  (79:79:79:a1:c8:f6).
[0665] [0] BLE : Discovered unknown device:  (34:fd:6a:1d:94:ea).
[0666] [0] BLE : Discovered unknown device:  (1c:b3:c9:17:76:b5).
[0667] [0] BLE : Discovered unknown device:  (de:0e:c4:b1:4c:d8).
[0668] [0] BLE : Discovered unknown device:  (dc:ff:06:b6:64:83).
[0669] [0] BLE : Discovered unknown device:  (66:a6:a6:3f:fa:8f).
[0670] [0] BLE : Scanning for 1 hub(s) aborted.
[0671] [0] BLE : Connecting to hub '90:84:2b:20:95:76'...
[0672] [0] BLE : Max clients reached - no more connections available.
[0673] [0] Loop: Connect failed. Will retry...
[0674] [0] BLE : Scanning for 1 hub(s)...
[0675] [0] BLE : Discovered unknown device:  (70:b1:3d:a7:ad:49).
[0676] [0] BLE : Discovered unknown device:  (56:e9:7b:07:6b:d5).
[0677] [0] BLE : Discovered unknown device:  (15:cc:91:53:95:e5).
[0678] [0] BLE : Discovered unknown device:  (79:97:14:43:08:22).
[0679] [0] BLE : Discovered unknown device:  (1c:b3:c9:17:76:b5).
[0680] [0] BLE : Discovered unknown device:  (53:e7:b0:2c:4d:e4).
[0681] [0] BLE : Discovered unknown device:  (de:0e:c4:b1:4c:d8).
[0682] [0] BLE : Discovered unknown device:  (79:79:79:a1:c8:f6).
[0683] [0] BLE : Discovered unknown device:  (dc:ff:06:b6:64:83).`
Hilbert70 commented 1 year ago

Joos, Please check if this solution works:

// Called for each advertising BLE server.
void BLEDeviceCallbacks::onResult(NimBLEAdvertisedDevice *advertisedDevice)
{
    // We have found a device, let's see if it has an address we are looking for.
    for (BLEHub *hub : _hubs) {
        if (advertisedDevice->getAddress().equals(*hub->_config->DeviceAddress)) {
            log4MC::vlogf(LOG_INFO, "BLE : Discovered hub: %s (%s).", advertisedDevice->getName().c_str(), advertisedDevice->getAddress().toString().c_str());

            hub->_advertisedDevice = advertisedDevice;
            hub->_isDiscovered = true;

            return;
        }
    }

    log4MC::vlogf(LOG_INFO, "BLE : Discovered unknown device: %s (%s).", advertisedDevice->getName().c_str(), advertisedDevice->getAddress().toString().c_str());
    NimBLEDevice::addIgnored(advertisedDevice->getAddress());
}
Mattzobricks commented 1 year ago

Tested by Mattze yesterday - works well.

Hilbert70 commented 1 year ago

Limit was user error, the ignoring did not solve it, but is a nice improvement. The user my_platformio.ini overruled the one in platformio.ini. I think we could close this one.

Mattzobricks commented 1 year ago

Thanks, @Hilbert70.