Android Question Buffer help

ales2701

New Member
Licensed User
i all
i have a problem with buffer , i work with two device connect with bluetooth mode, that are send a simple json string.
i explain the problem.
the device sends to the buffer pieces of string in json, as sequentially '{"node": 1,' the next piece of string will be '' device 'd3', until complete the entire string json {"node1" : 1, "device": "d350", "temperature": 20, "humidity": 70}
the problem exists when control the formation of the string json with the log of B4A, it happens sometimes that in forming the string, the buffer does not send anything more and starts returning a new string adding it to the previous example:
{"node1": 1, "device": "d350", "tem {" node1 ": 1," device ":" d350 "," temperature ": 20," humidity ": 70}.
when I have to parse the json, the program goes in error because it is not a string json correct.

this is the part of program for create the entire string json.

thanks!!

B4X:
Sub Astreams1_NewData (Buffer() As Byte)
    Dim u As String
    Dim lpos As Long
    Dim rpos As Long
    u=(u&BytesToString(Buffer, 0, Buffer.Length, "UTF8"))   
    str_ = str_ & u
    Log ("stringa lunghezza" & str_.Length)
    Log ("string base " & str_)
    If str_.Length > 160 Then
        lpos=str_.IndexOf("{")
        rpos=str_.IndexOf2("}",lpos+1)
        If lpos < 0 Then
            Log("lpos negativo ----------------------------------------------")
            str_=" "
        End If       
        If lpos>=0 Then
            If rpos > lpos Then
                If str_.Length >170 Then
                    str_ = "" ' for delete the string'
                Else   
                    s = sf.Mid(str_,lpos+1,(rpos+lpos)+1) 'change -1'
                    json_interpreter1(s)    'change the buffer'
                    Log ("json string" &s)
                    Log("Lenght" & S.Length)
                    str_=sf.Right(str_,(str_.Length-rpos)-1)
                End If 
            End If
        End If
       
    End If
End Sub
 
Top