iOS Question Non-contiguous string received via BLE

doodooronron

Member
Licensed User
I am trying to receive a 52 byte data string from a BLE module using the BLECentral app, but the string arrives in out-of-sequence 20-byte blocks.

The test string I am sending from the module is "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

The string that is received is "UVWXYZabcdefghijklmnABCDEFGHIJKLMNOPQRSTopqrstuvwxyz", which is Block2 & Block1 & Block3.

The string is received properly by a BLE terminal app on an Android phone.

I'd rather not just arbitrarily shuffle the blocks around without knowing what is going on.

Is there a correct method for receiving strings longer than the 20 byte BLE nominal limit without them being shuffled like this?

I'm using an iPhone 4S.
 
Last edited:

doodooronron

Member
Licensed User
OK, after a good night's sleep, I've got a solution, although I still don't know why the strings are out of sequence in the BLECentral textbox, I can now extract the whole 52-byte string in the correct order with just a small tweak of Erel's original code.

B4X:
Private Sub Manager_DataAvailable (SId As String, Characteristics As Map)
    If firstRead Then
        manager.SetNotify(ServiceId, ReadChar, True)
        firstRead = False
        Return
    End If
    Dim b() As Byte = Characteristics.Get(ReadChar)
    NewMessage(b)
End Sub

Sub NewMessage (Data() As Byte)
    Basket = Basket & BytesToString(Data, 0, Data.Length, "utf8")
    Log(Basket)           'Hooray!
    txtLogs.Text = Basket & CRLF     'Hooray!
End Sub

Unfortunately, this will not work as is, because the string "Basket" will continue to grow as the new data gets concatenated onto the end, I need to find a way to parse the string as soon as the 0x0D0A (CRLF) bytes that are in the transmitted string are received, then clear the "Basket" string ready for more data.

There should be 2 new rules added to the Forum rules - "Sleep before posting", and "Test your 'solution' before posting".
 
Last edited:
Upvote 0

doodooronron

Member
Licensed User
Update: Now works by testing for a line feed. If one is found, the parsing subroutine is called to process the buffer string "Basket" which then clears the buffer, ready to build the next message string.

B4X:
Sub NewMessage (Data() As Byte)
    Basket= Basket & BytesToString(Data, 0, Data.Length, "utf8")
    For Pos = 0 To Data.Length - 1
        If Data(Pos) = 10 Then
            If Basket.Length > 2 Then
                Parse
            End If
        End If
    Next
End Sub
 
Upvote 0
Top