react-native-ble-plx icon indicating copy to clipboard operation
react-native-ble-plx copied to clipboard

monitorCharacteristicForService limit?

Open cyberdevnet opened this issue 2 years ago • 5 comments

Hi,

I'm monitoring a characteristic using monitorCharacteristicForService using this code:

        device.monitorCharacteristicForService(
          SERVICE_UUID,
          PGM_NUMBER_CHARACTERISTIC_UUID,
          (error, characteristic) => {
            if (error) {
              console.log('error', JSON.stringify(error));
            }
            if (characteristic?.value != null) {
              let PMGreturn = base64.decode(characteristic.value);
              console.log('🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn:', PMGreturn);
            }
          },
          'PGM_NUMBER_CHARACTERISTIC_UUID'
        );

My BLE Server is sending chunks of data in a loop, the number of chunks can vary.

The strange behaviour is, that I'm always getting no more than 17 chunks, here what I'm logging:

 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 1115","SongID":"1115"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 1231","SongID":"1231"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 1376","SongID":"1376"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 1525","SongID":"1525"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 1583","SongID":"1583"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 1674","SongID":"1674"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 1691","SongID":"1691"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 1821","SongID":"1821"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 1861","SongID":"1861"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 2332","SongID":"2332"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 2529","SongID":"2529"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 2763","SongID":"2763"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 2788","SongID":"2788"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 2789","SongID":"2789"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 2916","SongID":"2916"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 2936","SongID":"2936"}
 LOG  🚀 ~ file: App.js:1120 ~ .then ~ PMGreturn: {"songName":"Song 3172","SongID":"3172"}

Is there any kind of limitation in monitorCharacteristicForService?

I tried also with other characteristic and various kinds of data, I can get no more than 17 response.

I'll upload debug logs if needed.

cyberdevnet avatar Aug 23 '23 10:08 cyberdevnet

I'm not sure how your device works. Is the data sent as notification or indication (monitorCharacteristicForService checks which method of connection is possible)? Yes, it is possible to lose some packet during the connection and connection throughput is limited. https://community.silabs.com/s/article/ble-master-slave-gatt-client-server-and-data-rx-tx-basics?language=en_US https://infocenter.nordicsemi.com/index.jsp?topic=%2Fsds_s132%2FSDS%2Fs1xx%2Fble_data_throughput%2Fble_data_throughput.html

Hi @dominik-czupryna-withintent , I'm using Notification, here the relevant code:

void sendChunksWithFlags(const String& data, BLECharacteristic* characteristic, int maxChunkSize)
{
    int numChunks = (data.length() + maxChunkSize - 1) / maxChunkSize;

    // Send the "startTXflag" flag separately
    characteristic->setValue("startTXflag");
    characteristic->notify();
    delay(100); // Introduce a delay before sending chunks

    for (int i = 0; i < numChunks; i++) {
        int startIdx = i * maxChunkSize;
        int endIdx = startIdx + maxChunkSize;
        if (endIdx > data.length()) {
            endIdx = data.length();
        }

        String chunk = data.substring(startIdx, endIdx);

        // Set the characteristic value and notify for each chunk
        characteristic->setValue(chunk);
        characteristic->notify();

        // Introduce a small delay between chunks to avoid flooding the Bluetooth stack
        delay(100);
    }

    // Send the "endTXflag" flag separately after sending all chunks
    characteristic->setValue("endTXflag");
    characteristic->notify();
}

I'll try to play with the connection intervals

https://punchthrough.com/ble-throughput-part-4/

cyberdevnet avatar Sep 29 '23 09:09 cyberdevnet

I tried to reproduce this problem using an nRF stimulator but everything seems to work fine. However, in this simulator the minimum interval is 100ms. Did changing the intervals help you or does the problem still occur?

Hi, I didn't try yet, I'm a bit stuck with other projects but I'll update you asap. Thanks for testing anyway

cyberdevnet avatar Oct 12 '23 09:10 cyberdevnet

@dominik-czupryna-withintent I tried again but unfortunately same result, I tried to increase the minimum interval of the server up to 150ms but still the same. I tried nRF too and i get everything there.

I even update to 3.1.0

cyberdevnet avatar Oct 21 '23 21:10 cyberdevnet