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

Subscription to Notification stops working 2.0.0

Open danbITC opened this issue 11 months ago • 4 comments

The notifications will work initially, but it seems they timeout or just stop functioning at some point. I think it happens after 4-5 minutes.

I am running this code on an ESP32-S3

I setup the notifications like this (taken from an example on this repo)

if (pRead->canNotify()) {
        if (!pRead->subscribe(true, notifyReadCB)) {
            pClient->disconnect();
            LV_LOG_USER("Failed to subscribe to read characteristic");
        }
    }

The notifyReadCB looks like this. Modified from an example on this repo.

    // LV_LOG_USER("Notify/Indicate callback called");
    // LV_LOG_USER("isNotify: %s", isNotify ? "true" : "false");
    std::string str = (isNotify == true) ? "Notification" : "Indication";
    str += " from ";
    /** NimBLEAddress and NimBLEUUID have std::string operators */
    str += std::string(pRemoteCharacteristic->getClient()->getPeerAddress());
    // str += ": Service = " + std::string(pRemoteCharacteristic->getRemoteService()->getUUID());
    // str += ", Characteristic = " + std::string(pRemoteCharacteristic->getUUID());
    std::string dataStr;
    for (size_t i = 0; i < length; ++i) {
        char buf[4];
        snprintf(buf, sizeof(buf), "%02x", pData[i]);
        dataStr += buf;
        if (i < length - 1) {
            dataStr += " ";
        }
    }
    str += ", Value = " + dataStr;
    LV_LOG_USER(str.c_str());
    receiveBluetoothMessage(pData, length);
    // LV_LOG_USER("Connection Interval: %d    Connection Latency: %d    Connection Timeout: %d", conn.getConnInterval(), conn.getConnLatency(), conn.getConnTimeout());
}

The connection happens successfully, the connection parameter negotiation happens successfully. I don't see a disconnect event.
I know that the device that I am connecting to works normally with a phone app, so I don't believe that its something wrong with the device itself. It's a nordic softdevice (S132) which seems fairly mature.

I'm not quite sure where to start looking to resolve this issue as I am not intimately familiar with BLE protocols. If I could be pointed in the right direction I may be able to help resolve this with a PR. Do I need to have some sort of a heartbeat message to keep the connection alive?

danbITC avatar Dec 04 '24 15:12 danbITC

2.0.0 was an accidental release and is still pending removal from the library manager unfortunately. I suggest reverting back to 1.4.3 until 2.0.0 is officially released with the documentation to help with this issue (many things have been changed).

h2zero avatar Dec 04 '24 15:12 h2zero

Understood, I am intentionally developing with 2.0.0 to better understand all of the changes needed to make the upgrade and not using the library manager.

danbITC avatar Dec 04 '24 15:12 danbITC

Okay, that's cool 😄. Do you have another device you can test with, perhaps an esp32?

h2zero avatar Dec 04 '24 15:12 h2zero

I do not, but I was able to band-aid the issue for now by sending a message every two minutes to trigger the notification.

danbITC avatar Dec 09 '24 15:12 danbITC