Would have been good if you had shown your working example. I've read everything and still don't get it. I'm slower than you.
Because my solution wasn't a solution for Astream.write.
I ended up giving up on it. However if a sequence of nested subroutines/wait fors have to complete in a specific order at a specific time? I just simply return a value even if its not used. like return a true or false or something, then discard it. This may get optimized out? I dont know but it has worked so far.
but astreams.write happens asyncronously in the background which causes problems if i need to send packets back to back in a specific timing window. Even if I use a astream.write, sleep(x) and astream.write. It still doesn't work and sometimes packets will get rammed together back-to-back or skipped entirely. Its very weird.
Sadly, I had to use a push timer for this. Throw packets in a FIFO and bytesbuilder, and a 100mSec timer takes care of it. It just checks the queue, sends it, and returns.
This works for blasts, but the problem is if you need 2-way communciation and you have to wait on a response before sending another packet.
It is very painful and not good for scenarios like this. I do not know of an alternative library either. Also if you are dealing with another protocol over bluetooth/serial thats not compatible with Prefix, it gets even more messy because not all of the packet arrives at the same time for an Event fire, or sometimes a packet and a half can arrive during the trigger.
Then it gets difficult to handle because now you need to treat the buffer like a FIFO where you check for the entire packet, do something with it and "pop that off" while the event is filling it in the background simultaneously. Popping off a packet requires you to know the header/footer boundaries and handle that manually... and ive still not mastered that art yet.