Android Question AsyncStream NewData event queuing problem and Timer question

VBAPro

New Member
Licensed User
Longtime User
Hi Erel,


I guess I should have opened a new thread since my question is somewhat different than the thread I originally added my question to. You can read my original question here:

http://www.b4x.com/android/forum/threads/async-streams-interactive-communication.14523/#post-208551

It's a long post so I'm not going to copy it and paste it here as well, but there have been some new developments.

Basically I am trying to get my two devices to keep sending messages to each other in response to what the users are doing while the main loop is going.

I also want the devices to display some Toast messages (and calculate the score) in response to the newly arrived data from each other. Both devices are running the same code.

And all of this is supposed to happen while the main loop (the actual interactive game) is going on both devices.

I now use the prefix when I am initializing the AStream, but I don't care how many bytes have arrived - I just want both devices to respond as soon as anything arrives from the other device, because I am just sending simple, short text messages.

I added a new sub which gets called at each iteration of the main loop, and it looks like this:

B4X:
Sub CheckAStream

Dim rcv_bytes as Long
Dim oqs as Int
Dim rcv_msg as String

rcv_bytes = AStream.StreamReceived
oqs = AStream.OutputQueueSize
If oqs > 0 Then

    ToastMessageShow("Output Queue size: " & oqs, False)

End If
If rcv_bytes > 0 Then

    ToastMessageShow("Received bytes: " & rcv_bytes, False)   
    TR.Initialize(serial1.InputStream)
    rcv_msg = TR.ReadLine   
    ToastMessageShow("Received message: " & rcv_msg, False)   
    TR.Close

End If
End Sub

None of these Toast messages ever appear. It's like there's no output queue and no received bytes until the end of the game when all of the events that got queued up during the main loop (instead of triggering during the main loop) start coming through. And the main loop is large enough that I probably shouldn't use DoEvents.

And now I have another question. I thought the Timer was defined with each tick equal to 1 millisecond. Or so it says here:

http://www.b4x.com/android/wiki/index.php/Timer

But I have a HTC Wildfire S, and a Ulefone U650, and the game runs for about 90 seconds in total. Well, the Ulefone escapes the HTC by 4 or 5 seconds over the period of 90 seconds, and I am running the same code on both of them. I know that the Ulefone is brand new and much faster, but still... a Timer really should be the same on both devices. Or could I be doing something wrong? Here is my Timer code:

B4X:
Timer1.Initialize("Timer1", 1000) ' 1000 = 1 second
Timer1.Enabled = True

Sub Timer1_Tick()

tick_seconds = tick_seconds + 1
tick_minutes = tick_seconds / 60
left_seconds = tick_seconds Mod 60

If left_seconds < 10 Then   
    TimerLabel.Text= "0" & tick_minutes & ":0" & left_seconds
Else
    TimerLabel.Text= "0" & tick_minutes & ":" & left_seconds
End If

End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are not working with AsyncStreams correctly. Also in Android there is no such thing as a "main loop". You shouldn't hold the main thread in a loop.

I recommend you to start with this tutorial: AsyncStreams Tutorial

And also see some other tutorials that are based on AsyncStreams: http://www.b4x.com/search?query=AsyncStreams
 
Upvote 0
Top