Android Question Extracting data from asyncstream

andyp

Member
Licensed User
Hi

I am using this example https://www.b4x.com/android/forum/threads/hc-05-classic-bluetooth.66677/#content (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!
 

Attachments

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

emexes

Expert
Licensed User
???
B4X:
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
    loop
end sub
 
Upvote 0

emexes

Expert
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:
B4X:
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.

    ArdGpsBuffer.Initialize
End Sub
then handle data as it comes in from the Arduino (doesn't matter whether arrived by Bluetooth or USB):
B4X:
Sub ArdGpsHandleBytes(NumBytes As Int, Bytes() As Byte)
 
    For I = 0 To NumBytes - 1
        ArdGpsHandleByte(Bytes(I))
    Next
 
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
                ArdGpsHandlePositionLine(L)
            Else if UCL.Contains("ALTITUDE") Then
                'ArdGpsHandleAltitudeLine(L)
            else if UCL.Contains("SPEED") Then
                ArdGpsHandleSpeedLine(L)
            'next line example of coping with various misspellings of "satellite"
            else if UCL.Contains("SAT") And UCL.Contains("LITE") Then
                'ArdGpsHandleSatelliteLine(L)
            '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
                'ArdGpsHandleDirectionLine(L)
            else if UCL.Contains("TIME") Then
                'ArdGpsHandleTimeLine(L)
            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:
B4X:
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
            ArdGpsHandleSpeed(Speed)
        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
Top