Android Question When does AsyncStreams_NewData event happen?

DIYRicey

New Member
I am sending Bluetooth messages from an ESP32 module to a phone. The messages are short strings like "123" or "-123" I am listening out for them with the NewData event of an asyncstreams sub. I have initialized the asyncstreams object in non-prefix mode. If I send "123" the event occurs after the three characters have been received. If I send "-123" I usually get two events: one after the "-" is received and then another after the "123", although sometimes it waits until all four characters have been received, in which case I receive the proper message.

How do I ensure the NewData flag is not raised early?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
If you aren't using prefix mode then you cannot assume that messages will arrive as a single event. You should expect messages to be split or merged.
If there is a different character that separates messages, such as new line character, then you can use AsyncStreamsText.

B4XBytesBuilder can be useful as well. It will allow you to collect the pieces and then find full messages, based on some logic that you will supply.
 
Upvote 0

emexes

Expert
Licensed User
The messages are short strings like "123" or "-123"

If you're just sending a stream of integer values with a range (max - min) of less than 256, then why not just send the readings as single bytes?

Based on your example readings of 123 and -123, you might even just be able to use the Byte type directly, no offsetting required

(as in if eg your readings ranged from 32 to 212, then you'd need to adjust them in some way to fit into Byte's -128 to 127 range, eg by subtracting 100)
 
Last edited:
Upvote 0

DIYRicey

New Member
Thanks for the advice. I had implemented a solution using prefix mode AsyncStreams but I think AsyncStreamsText sounds like a simpler, promising option.
 
Upvote 0
Top