Android Question AsyncStreams NewData event is freezing app

Alessandro71

Well-Known Member
Licensed User
Longtime User
My app processes incoming serial bluetooth data in a foreground service.
The NewData sub event handler is defined in the service module.
It looks like when there is a massive amount of incoming data, the whole app freeze.
I suspect the culprit is the NewData event processing, that called repeatedly under load.
I already tried adding a Sleep(0) in the NewData sub, with no different result.
How can I give back control to main thread to be sure the app is not marked as not responding?
 
the Sleep call was just a test...
here is the NewData code

Code: Copy
' this event is activated when we receive something
' we will do something only after receiving a ">"
' that means the answer is complete
Sub OBDStream_NewData(Buffer() As Byte)
    Try
        Dim rsp As String
        rsp = BytesToString(Buffer, 0, Buffer.Length, "UTF8")

        response.Append(rsp)
       
        Dim PromptReceived As Boolean
        PromptReceived = rsp.Contains(">")

        If (PromptReceived) Then
            'prompt is not terminater with CR, so we add it
            response.Append(Chr(13))
            'this doesn't count as a received response
            RequestsPerSecond = RequestsPerSecond - 1
        End If

        'log received bytes
        Dim tmp As String
        Dim x As Int
        x = 0
        Do Until x = -1
            tmp = response.ToString
            x = tmp.IndexOf(Chr(13))

            response = response.remove(0, x + 1)
            If (x >= 0) Then
                Dim frame As String

                'remove CR
                frame = tmp.SubString2(0, x)

                WriteOBDLogReceived(frame)
                RequestsPerSecond = RequestsPerSecond + 1
            End If
        Loop

        'we received a prompt
        If PromptReceived  Then
            SendNextCommand
            response.Initialize 'clear string buffer
        End If
    Catch
        util.HandleException("sequencer.OBDStream_NewData", util.EXCEPTION_CRITICAL)
    End Try
End Sub

i'm currently looking into WriteOBDLogReceived which actually contains call to the activity which displays received text in an EditText which mimics a scrolling terminal

Code: Copy
'send string to display of dash activity
Private Sub SendToTerminal(text As String)
    CallSub2(Dash, "DisplayLine", text)
End Sub

may it flood the message queue, causing app not to respond anymore?
 
Upvote 0
Why aren't you using AsyncStreamsText?

Interesting suggestion, I will look into it, but it should handle occasional null bytes
As a follow up I added a flood control to the SendToTerminal CallSub2 call
It will be skipped if more than 50 call/sec are performed.
So far it looks like the app has a better response behaviour.
 
Upvote 0
Back
Top