Android Question Clearing a Bluetooth buffer

patrick14384

Member
Licensed User
Longtime User
Sorry to say I am struggling with this, hopefully someone can assist me with this. The application is reading in a prefix value via bluetooth, and all works just fine except if I wait say 30+ seconds, a queue builds up data that I do not want before the user clicks the "Graph" button. I am trying to block/flush/ignore this data, but my attempts are not getting anywhere. This is all in a service that gets started as soon as the "graph" button is clicked, the connection has already been established.
B4X:
Sub Service_Start (StartingIntent As Intent)
    If AStream.IsInitialized = False Then
        'Basic for Android call
        AStream.InitializePrefix(Main.serial1.InputStream, False, Null, "AStream")
        Log("Initialize Bluetooth data")
        Log("StreamReceived is currently: " & AStream.StreamReceived)
        Log("StreamTotal is currently: " & AStream.StreamTotal)

    End If   
end sub

and then this gets called
B4X:
Sub AStream_NewData (buffer() As Byte)
    Dim Incoming As String
    Dim Start As Int
    Dim Stop As Int

    'Determine if the graph is paused, and if it is not paused (we are graphing actively) then accept the incoming data
    If Main.Graph_Paused = False Then
        Log("Buffer Length: " & buffer.Length)
        Incoming = BytesToString(buffer, 0, buffer.Length, "UTF8")
        Start = Incoming.IndexOf("B")
        Stop = Incoming.IndexOf("Q")
        Log("Incoming:" & Incoming)
        'Log("Start:" & Start)
        'Log("Stop:" & Stop)
    End If
End Sub

So my question is
1) Am I doing something wrong initially?
2) If not, how can I determine how many messages are stored currently so that I can discard them?

Thanks!
Patrick
 

patrick14384

Member
Licensed User
Longtime User
I am pretty sure I just fixed this after staring at it for another hour. I was starting the service in the main thread just before I wanted to start graphing figuring no need to have the service running until absolutely the last second. But for some reason it starts collecting data as soon as a connection is established, so I was getting a lot of junk I did not want.

Let me try to describe this better for future reference possibly. As soon as I ran StartService then AStream_NewData would cycle thru 10-30 time immediately, depending on how long I waited after establishing a connection instead of the once every 2 secs it was supposed to.

The fix was to start the service earlier, and in Astream_NewData discard the incoming data until the user presses the graph button.

I still don't know how the messages were getting queued, maybe something on the Android side (I have a Nexus 10 with 4.4.2 if that matters at all). Anyways, I am good now. Thanks for the assistance.
 
Upvote 0
Top