Only last callback is called when there're multiple writes
Version
Tell us which versions you are using:
- react-native-ble-manager v6.2.4
- react-native v0.50.1
- iOS/Android iOS 11
Expected behaviour
When there're multiple writes, there should be multiple corresponding callbacks.
Actual behaviour
If multiple writes are writing to the same peripheral and same characteristic, the later callback will overwrite the earlier one.
Steps to reproduce
- Trigger multiple writes to the same peripheral and same characteristic
Stack trace and console log
https://github.com/innoveit/react-native-ble-manager/blob/master/ios/BleManager.m#L435
Here the key is peripheral uuid + characteristic uuid, so it's overwritten.
I'm not sure if didWriteValueForCharacteristic is always called in the same order as writeValue: forCharacteristic:. Suppose the writes on native side is queued, I think the order should be same. If so, we can change the value writeCallbacks to array (queue) of callbacks instead of single callback so when didWriteValueForCharacteristic, pop and invoke the callback.
Another solution or maybe better solution is queue the writes up, either on JS side or native side, so before the last write is finished, do not start processing the next write.
I'll propose a native side solution with a pr.
Also, when reading values, the callback is called in:
https://github.com/innoveit/react-native-ble-manager/blob/master/ios/BleManager.m#L59
But when there's any error, the method returns, without calling and cleaning up the readCallbacks, it'll cause a pending Promise on JS side.
Hi, now you have to handle the queue in the js side.
@marcosinigaglia do you have any best practice of queueing writes?
Btw, as mentioned above, I think the readcallback should still be called when there's error occurred.
It depends how you manage the app, in my case I put the messages in a queue using redux. I'll check for the error in the reading.
However in my personal opinion, if queueing is necessary for the writes working properly, it should be in the lib, either on native side or in BleManager.js, rather than leaving it to the app.
In my use case, after connecting a device, I'll send some commands to it for data syncing, for example something like:
async connect(peripheral) {
const pid = peripheral.id;
await BleManager.connect(pid);
const peripheralInfo = await BleManager.retrieveServices(pid);
await BleManager.startNotification(pid, SERVICE_UUID, CHAR_UUID);
await BleManager.write(pid, SERVICE_UUID, CHAR_UUID, command_1);
await BleManager.write(pid, SERVICE_UUID, CHAR_UUID, command_2);
await BleManager.write(pid, SERVICE_UUID, CHAR_UUID, command_3);
}
That's when I noticed sometimes some commands never calls back, neither then nor error. It looks weird to me because the number of callbacks is less then the number of total Promises, and it's unpredictable and unexpected.
@marcosinigaglia
Hi, I am having the problem that startNotification might get stuck on Android, maybe it is because I proceed to next operation before the callback.
Can you show me how you use redux to put the operations (like startNotification) into queue and then execute them safely (wait for the callback)?