dotintent / react-native-ble-plx

React Native BLE library
Apache License 2.0
3.05k stars 514 forks source link

Loosing data sent from a BLE device #903

Closed david-gettins closed 11 months ago

david-gettins commented 3 years ago

Prerequisites

Please answer the following questions for yourself before submitting an issue. YOU MAY DELETE THE PREREQUISITES SECTION.

Expected Behavior

All data received from a GATT server is accessible.

Current Behavior

With high update rates some data is missing. Not sure if it is due to bridge communication or too many callbacks queued or something else. This also happens at lower update rates but more characteristics notifying.

I have tried a few different approaches e.g. write staright to MMKV storage or buffer in an array then periodically save to storage.

Below are ECG (EKG) traces generated from a health and fitness device with an update rate of 2048Hz.

This is from an RN app using this library: image

This is from a native app: image

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Get a BLE device / GATT server that has a characteristic that emits data >=2048Hz.
  2. Start monitoring the characteristic - storing in some kind of store
  3. Plot the recieved data on a chart for analysis.

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions. Both JS and platform logs can be enabled via setLogLevel function call.

ble.notify - simply wraps the usual function so the consumer doesn't need to know about the ble manager

export function notify(
  deviceId: string,
  serviceId: string,
  characteristicId: string,
  callback: (
    error: BleError | null,
    characteristic: Characteristic | null,
  ) => Promise<void> | void,
): Subscription {
  return bleManager.monitorCharacteristicForDevice(
    deviceId,
    serviceId,
    characteristicId,
    callback,
  );
}

start notifications and write to a raw data store

let index = 0;

const sub = ble.notify(deviceId, service, characteristic, (error, char) => {
  if (error && error.errorCode !== BleErrorCode.OperationCancelled) {
    logger.warn(error);
    return;
  }

   if (char && char.value) {
    rawData.push(char.uuid, startDate, char.value, index++);
  }
});

monitored.push({ sub, uuid: characteristic, date: startDate });

rawData.push - using react-native-mmkv for the storage module

export function push(
  uuid: string,
  date: string,
  value: string,
  index = 0,
): void {
  const key = `${uuid}::${date}::${index}`;
  storage.storage.set(key, value);
  storage.storage.set(`${uuid}::${date}::latest`, index);
}
snydersaurus commented 2 years ago

Did you ever get to the bottom of this @david-gettins ?

david-gettins commented 2 years ago

Yes, well sort of... On Android you can reduce the data loss by setting connection priority to high:

bleManager.requestConnectionPriorityForDevice(device.id, ConnectionPriority.High)

Although, I still found data loss but only at around 0.6%. This could be seen as acceptable for most cases but presents a bit of an issue if your trying to do something with ECG signals (or similar) e.g. R peak detection, as the missing data points could be your R peak.

I messed around for sometime, even created a new app with nothing going on but the data collection to no avail. I have now created my own native module with Android and iOS implementations that save raw data on the native side using cooroutines (Android) and a dispatch queue (iOS). Only the data I need for the UI gets parsed and sent across to React Native. With this set up I could reduce the number of calls across the RN bridge by sending an event with the UI relevant data every 1 second. This was sufficient for the UI and no raw data is lost.

snydersaurus commented 2 years ago

Thanks - I had a hunch that is what you would say. Glad you were able to get things working over the bridge.

abursztynowska commented 1 year ago

I'm excited to inform you that we've released a new version of react-native-ble-plx. Many of the concerns and bugs have been addressed in this update.

If you encounter any issues or have further suggestions, please don't hesitate to open a new issue.