abandonware / noble

A Node.js BLE (Bluetooth Low Energy) central module : Community maintained
https://libraries.io/npm/@abandonware%2Fnoble
MIT License
538 stars 162 forks source link

Question about write reliability / 'resend' mechanism if package was lost. #295

Open andresbrocco opened 1 year ago

andresbrocco commented 1 year ago

I'm controlling a peripheral by writing to certain characteristics at a rate of 5 Hz. Those characteristics are 'writable' and not 'writable without response'.

1) What happens to the adapter if no response is received (for example if a package is lost)

2) Ideally if no answer is received (in 50ms, for example), I would try and re-send the previous message. How could I implement that behaviour?

Example:

characteristic.write(data, false, async (error) => {
    if (error) {
        < I would try to re-send the same data here >
    } else {
        < everything went fine >
    }
});

In my example code I'm not sure where/how could I implement a 'resend' mechanism if the callback is never called when no response is received.

I thought about using Promise.any() like the following:

Promise.any([characteristic.writeAsync(data, false),
             new Promise((resolve, reject) => { setTimeout(() => reject(new Error('timeout')), 20) })]
).then(() => {
    < everything went fine >
}).catch((error) => {
    < try to re-send the data >
});

But that does not seem to work either...

3) Am I in a good direction? Or are there any better solutions to that simple problem of ensuring the delivery of a package?

I would be grateful for any light on my problem!

andresbrocco commented 1 year ago

I was supposed to use Promise.race() instead of Promise.any(), now it's working... But that doesn't answer the question 1).