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

How do I READ or MONITOR the characteristic synchronously after every WRITE?

Open tymoxx opened this issue 4 years ago • 0 comments

I need to make a series of WRITEs to one characteristic, but before every other WRITE, I need to READ the response value in another characteristic.

My code works 90% of the time. But sometimes it breaks because the READ-characteristic is not updated (it still shows a previous value). Probably because the device is not quick enough to update the read-characteristic.

Here is a simplified pseudo-code I wrote:

// Write to the device
await device.writeCharacteristicWithResponseForService(SERVICE_UUID, WRITE_CHARACTERISTIC_UUID, createCommand(value));

// Read the response from characteristic
const VALUE_1 = await device.readCharacteristicForService(SERVICE_UUID, READ_CHARACTERISTIC_UUID);

// Write to the device again where I include the read value
device.writeCharacteristicWithResponseForService(SERVICE_UUID, WRITE_CHARACTERISTIC_UUID, VALUE_1);

const VALUE_2 = await device.readCharacteristicForService(SERVICE_UUID, READ_CHARACTERISTIC_UUID); // PROBLEM: sometimes VALUE_2 is not updated and I still get a VALUE_1 here.

// Write to the device again where I include the read value
device.writeCharacteristicWithResponseForService(SERVICE_UUID, WRITE_CHARACTERISTIC_UUID, VALUE_2);

// etc... 

Please advise how you would achieve synchronous communication between the BLE device and the phone. Note that the READ characteristic is also a MONITOR characteristic. Maybe it is better to monitor it?

tymoxx avatar Mar 30 '22 15:03 tymoxx