Android Question AsyncStreamsText, AsyncStreams

mr23

Active Member
Licensed User
Longtime User
Erel, Starting a new thread here (left the thread AtomicInteger).

I stripped down AST's astreams_NewData to the following, adding a "Lock" (DataInSem), and a counter iP as Int in Class_Globals.

B4X:
Private Sub astreams_NewData (Buffer() As Byte)
    DataInSem.Wait
    iP = iP + 1
    Dim sP As String = iP
    Dim s As String = BytesToString(Buffer, 0, Buffer.Length, charset)
    Log(sP & s)
    DataInSem.Unlock
End Sub

Then I logged some output, where I first let it run for bit, then hit a button and brought up a menu, waited a bit, then hit back-arrow to cancel, then logged what followed. Below is the log output.

The micro w/BT transmitting the data is sending about the same thing every time, with slight variation.
Basically: "B,H0000,V0127,F000,S000,D0,A0700,W12700,T10,E<CR><LF>" is repeated once a second from the micro.
As each partial string comes in the _NewData in Buffer(), Lock is taken, iP is incremented, converted to string, prepended to the incoming string conversion of buffer, the Logged, and lock released.

Log:
Installing file.
...
** Activity (main) Resume **
Serial connected: true
1B,H0000,V0127,F000,S000,D0,A0700,W12700,T10,E
2B
3,H0000,V0127,F000,S000,D0,A0000,W12
4700,T20,E
5B,H0000,V0127,F000,S000,D0,A0000,W1
62700,T20,E
7B,H0000,V0127,F000,S000,D0,A0000,W12700,T20,E
8B,H0000,V0127,F000,S000,D0,A0100,W12
9700,T30,E
10B
11,H0000,V0127,F000,S000,D0,A0100,W12
12700,T30,E
13B
14,H0000,V0127,F000,S000,D0,A0100,W12 < Button is hit right after this>
15700,T30,E
162700,T10,E
17000,V0127,F000,S000,D0,A0000,W12700
18B
19,T20,E
202700,T00,E
21B
22,H0000,V0127,F000,S000,D0,A0400,W1
232700,T00,E
24B
25,H0000,V0127,F000,S000,D0,A0700,W1
26000,V0127,F000,S000,D0,A0400,W12700,
27,H0000,V0127,F000,S000,D0,A0700,W1
282700,T10,E
29B,H0
30T00,E
31,H0000,V0127,F000,S000,D0,A0400,W1
32B
33B
34,H0000,V0127,F000,S000,D0,A0700,W1
35B,H0
362700,T10,E
37B
38,H0000,V0127,F000,S000,D0,A0000,W1
392700,T20,E
40B
41,H0000,V0127,F000,S000,D0,A0000,W1
422700,T20,E
43B
44,H0000,V0127,F000,S000,D0,A0100,W127
4500,T30,E

The phone app's button isn't pressed until line #14 is out. All is good to that point. The app menu comes up, I wait a few seconds, then hit back-arrow to cancel.
The output is not written to the B4A Gui 'log' window until the menu is cancelled.
Note the order of the text 'chunks', each numbered by iP, is now in order numerically of iP, but the string portions are out of order.
At output #33 the ordering returns to correct order.

Any ideas? It appears to me that AsyncStreams is delivering the substrings in the wrong order, when the App's GUI pulls up a menu.
Is the source for AsyncStreams (from RandomAccessFile) available to me?
I tried RAF 1.5.1, then 1.5.0. I didn't go back to January's RAF.

Edit: Modal Dialogs in the Activity will block events in AsyncStreams, resulting in out of order delivery of the message substrings (AsyncStreams will break up an incoming message as designed). Even if the AsyncStreams is in a Service.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
As I previously wrote the lock is not required and will not do anything. This code is always executed by the main thread.

Which menu do you show? Is this a different activity?

AsyncStreams sends the received data to the main thread message queue. If you show a modal dialog then the order might not be consistent (see AsyncStreams tutorial).

It is also recommended to receive the data in a service instead of an activity. When the activity is paused the messages will not be handled.
 
Upvote 0

mr23

Active Member
Licensed User
Longtime User
Same activity. Input List used for menu.
Edit: A Service implementation will not fix the problem. See post #9
I'll have to use a Service then, as I want an implementation that can guarantee order of delivery, in spite of App interactions.
When not implemented as a Service, there is some frequency/bandwidth limit as Michael found as in post #14.
Having just read that B4A-Bridge is implemented in B4A using a Service, and having read the source, I trust that a Service is required to provide some guarantees over just using a AsyncStream/Text with _NewData/Text handler in an App. The latter may be useful in some limited cases or as an example, but anything else needs a Service.

And I see in posts 21-22 the issue of modal dialogs is touched on, when not using a Service.

Any chance you could/would provide me the source to AsyncStreams ?

Thanks for your time.
 
Last edited:
Upvote 0

mr23

Active Member
Licensed User
Longtime User
I added the Service. Logging the serialized received data strings as received in Sub Streams_NewData (Buffer() As Byte).

After starting the BT serial connection to the micro, all is good at first, data is intact and correct order as received in Sub Streams_NewData (Buffer() As Byte).

If I push the App's Activity button and bring up a menu using InputList, the same 'scrambling' of order occurs as received in Sub Streams_NewData (Buffer() As Byte).

Log: (micro is transmitting 1Hz, B4A App button is pressed just after string portion numbered "10" is received.
** Activity (main) Resume **
** Service (btserialservice) Create **
** Service (btserialservice) Start **
BTSS: Service_Start
Connecting to linvor
with MAC 00:12:10:22:10:45
BTSS_Connected true
Connected to SerialService (Bluetooth)
BTSS Streams_ND 1 B
BTSS Streams_ND 2 ,H0000,V0000,F095,S095,D0,A0000,W000
BTSS Streams_ND 3 00,T50,E
BTSS Streams_ND 4 B
BTSS Streams_ND 5 ,H0000,V0000,F095,S095,D0,A0000,W00000,T50,E
BTSS Streams_ND 6 B
BTSS Streams_ND 7 ,H0000,V0000,F095,S095,D0,A0000,W00
BTSS Streams_ND 8 000,T50,E
BTSS Streams_ND 9 B,H0000,V0000,F095,S095,D0,A0000,W00
BTSS Streams_ND 10 000,T60,E
BTSS Streams_ND 11 ,H0000,V0000,F095,S095,D0,A127600,W
BTSS Streams_ND 12 00000,T-10,E
BTSS Streams_ND 13 ,H0000,V0000,F095,S095,D0,A0000,W00
BTSS Streams_ND 14 ,H0000,V0000,F095,S095,D0,A0000,W00
BTSS Streams_ND 15 000,T60,E
BTSS Streams_ND 16 00000,T-10,E
BTSS Streams_ND 17 B
BTSS Streams_ND 18 B
BTSS Streams_ND 19 000,T60,E
BTSS Streams_ND 20 ,H0000,V0000,F095,S095,D0,A127600,W
BTSS Streams_ND 21 B
BTSS Streams_ND 22 B
BTSS Streams_ND 23 B

Can I see the source for AsyncStreams ?
 
Upvote 0

mr23

Active Member
Licensed User
Longtime User
You should not use modal dialogs with AsyncStreams. It doesn't matter whether you are implementing it with a service or an activity.

I'm sorry but I cannot provide the code for AsyncStreams.

We posted simultaneously.
What else besides Modal Dialogs will interfere with AsyncStreams?
What interface below/before that can I use to keep data integrity ?
 
Upvote 0

mr23

Active Member
Licensed User
Longtime User
I was hoping to be able to prompt via modal dialog (such as the Dialogs library or InputList) to change settings while the stream and calculations are active, without mis-ordered data. I don't want to have to check every string message for integrity, and toss bad ones. I could more readily deal with lost complete messages, but that doesn't seem to be happening here. I could pause the stream in some fashion, but that is not necessarily enough by itself (ie, btnFunction_Click, first statement would call the BTserialstream (object, either internal class or as a service doesn't seem to matter here for this discussion) to pause it, then unpause after complete with the dialog action). But it wouldn't be very convenient to have to put these guards into every 'dialog' operation that could impact the integrity.

The application is an infrared sensor with real-time display, and a message rate at a few Hz, and only a few hundred bytes are transferred per.
That is just the first application. I have others in the queue that are similar in nature, not an imaging sensor, but also have frequent periodic data to display, measure, etc, not unlike the test samples I just posted. It works as long as I don't open a modal dialog to adjust a setting. What are my options in B4A?
 
Last edited:
Upvote 0

mr23

Active Member
Licensed User
Longtime User
To close out, my search of the forums found to date that there is no non-modal dialog library available. In fact there is discussion by at least one dialog library author of not providing such a thing. Not everyone wants fully customizable panel-based non-modal dialogs. I think there is a call for a library of reusable non-modal dialogs like the modal ones out there. But then, I'm brand new to B4A and pretty new to java, so feel free to mentor ;-).
 
Upvote 0
Top