Android Question Extracting data from asyncstream


Licensed User

I am using this example (just the android code) to connect my android app to an Arduino + GPS module + HC-05 BT module.

If I log the BT data arriving in the app, I see it all fine :)

I have a google map up and running in the app, but I need to extract the latitude and longitude from this asyncstream as it arrives.

Could someone please point me in the right direction as to the best way of doing this? I have attached a sample of the data I am receiving......

Thank you!


  • Bluetooth Terminal HC-05.txt
    14.3 KB · Views: 156


Licensed User
for each incoming block of characters from Bluetooth connection
    add incoming chars to AssemblyString (probably using StringBuilder, but Char array no problem either)
    while AssemblyString contains End-Of-Line character 
        extract SingleTextLine from AssemblyString (ie all chars up to and including End-Of-Line)
        if SingleTextLine starts with "Latitude=" and contains "Longitude="
            extract numbers and convert to form suitable for use in your program
            HandlePositionUpdate(Latitude, Longitude)
        end If
end sub
Upvote 0


Licensed User
I needed something to distract me from son having dropped $$$k sax today at blues boot camp, so... here's something to chew on :)

In Starter module, declare and initialize buffer:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim ArdGpsBuffer As StringBuilder
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.

End Sub
then handle data as it comes in from the Arduino (doesn't matter whether arrived by Bluetooth or USB):
Sub ArdGpsHandleBytes(NumBytes As Int, Bytes() As Byte)
    For I = 0 To NumBytes - 1
End Sub

Sub ArdGpsHandleByte(B As Byte)
    If B = 10 Or B = 13 Then    'we have a new line
        Dim L As String = ArdGpsBuffer.ToString    'get line (not including end-of-line byte/char)
        ArdGpsBuffer.Initialize    'prepare (clear) ready for next line
        If L.Contains("=") Then    'quick filter: only process valid-looking lines
            Dim UCL As String = L.ToUpperCase
            If UCL.Contains("LATITUDE") And UCL.Contains("LONGITUDE") Then
            Else if UCL.Contains("ALTITUDE") Then
            else if UCL.Contains("SPEED") Then
            'next line example of coping with various misspellings of "satellite"
            else if UCL.Contains("SAT") And UCL.Contains("LITE") Then
            'next line example of coping with various synonyms of "course"
            else if UCL.Contains("COURSE") Or UCL.Contains("HEADING") Or UCL.Contains("BEARING") Or UCL.Contains("DIRECTION") Then
            else if UCL.Contains("TIME") Then
            End If
        End If
    Else    'regular character, add to buffer
        ArdGpsBuffer.Append(Chr(Bit.And(B, 0xFF)))    'not sure what -ve Unicode does, nor game to find out
    End If
End Sub
and lastly, handle the various lines (but watch out for incomplete lines esp. at startup) , eg:
Sub ArdGpsHandleSpeedLine(L As String)
    Dim P As Int = L.IndexOf("=")
    If P <> -1 Then
        Dim NumberString As String = L.SubString(P + 1).Trim
        If IsNumber(NumberString) Then
            Dim Speed As Float = NumberString    'could be merged into next line, but separate casting feels safer
        End If
    End If

End Sub

Sub ArdGpsHandleSpeed(S As Float)
    Log("Speed = " & NumberFormat2(S * 1000000 / 60, 1, 1, 1, False) & " mm/m")
End Sub

Sub ArdGpsHandlePositionLine(L As String)
    Log(L)    'have left the fun bit for you ;-)
End Sub
Last edited:
Upvote 0