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

Finish writing messages in the write queue before moving on…

Open gtbX opened this issue 1 year ago • 1 comments

…to the next command.

If you call BleManager.write() multiple times without awaiting, there was a chance the message fragments could get interleaved and corrupted. This change keeps the current command active while the write queue is not empty, so that new writes don't clobber it.

I also created a test branch to verify this fix, but I had to make a bunch of changes to get it to run as a unit test. I don't know if those changes will break anything else, so I left them out. It's here if you're interested: https://github.com/perfectco/react-native-ble-manager/tree/feat/test

gtbX avatar Oct 01 '24 17:10 gtbX

Hi @gtbX , thank you for your work, if you confirm that you have tested it and it works correctly I will proceed with the merge.

marcosinigaglia avatar Dec 12 '24 10:12 marcosinigaglia

Hi @marcosinigaglia Sorry it took a while to get back to this, but I was able to reproduce the issue on a physical device, and verify the fix.

Essentially, calling write() too fast could cause corruption:

const payloadA = new TextEncoder().encode("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
const payloadB = new TextEncoder().encode("BBBBBBBBBBBBBBBBBBBBBBBB");
BleManager.write(peripheral.id, characteristic.service, characteristic.characteristic, Array.from(payloadA));
BleManager.write(peripheral.id, characteristic.service, characteristic.characteristic, Array.from(payloadB));

could result in some "B" bytes being received before all the "A" bytes. With the fix, all bytes are received in order, despite the lack of await. Verified by running that snippet in the example app on one device, while watching the receive log in nRF Connect on another.

gtbX avatar Aug 25 '25 23:08 gtbX

Is that possible to be launched alongside with apple devices too?

lucaswitch avatar Aug 26 '25 00:08 lucaswitch

I don't have an apple device handy to test, but I don't see why it wouldn't work. IIRC the iOS code didn't have the same issue.

gtbX avatar Aug 26 '25 16:08 gtbX