noble icon indicating copy to clipboard operation
noble copied to clipboard

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

Open andresbrocco opened this issue 2 years ago • 1 comments

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)
  • Does the adapter hang? (I mean, it gets busy and unable to send other messages)
  • Is there a timeout?
  • Does the callback get called with an 'error'? (I've tested and that seems not true)
  1. 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...

  1. 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 avatar Feb 17 '23 14:02 andresbrocco

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

andresbrocco avatar Feb 18 '23 12:02 andresbrocco