Android Question BLE Speed

alizeti

Member
Licensed User
I noticed that when I use an Android phone for BLE data transmission, the android start to lag pretty fast (couple of seconds) and then the app do not respond anymore!! Need to close kill the app!!

On the iphone, if I use and iphone 6 and more recent, no problem, but iphone 5s samething happen, but after a longer delay!!

On both I received the same size of data and on the iphone the message is cut in 3 parts, but on android it's separated in 10 message!!!

Any idea of the reason?!?!
 

alizeti

Member
Licensed User
This is the code for the iOS
B4X:
Sub bleRxManager_DataAvailable (Service As String, Characteristics As Map)
    Dim data() As Byte
    Dim counter As Int

    Sleep(0)
   
    If Not(notifyEnable) Then
        notifyEnable = True
        bleRxManager.SetNotify (bleServiceUUI, bleRxUUID, True)
        startListening.Text =  "Stop Listening"
    Else
        For Each id As String In Characteristics.Keys
           
            If(id = bleRxUUID) Then
                Log(Service)
                Log(id)
               
                data = Characteristics.Get(id)
               
                Log(bc.HexFromBytes(data))
               
                If data(0) = 0x55 And data(1) = 0xFF And dataState = 0 Then
                '    start recording data until next start frame
                    bufferRx = ""
                    nbOfObjects = data(2)
                    bufferRx = bc.HexFromBytes(data)
                    dataState = 1
                Else If data(0) = 0x55 And data(1) = 0xFF And dataState = 1 And bufferRx.Length > (nbOfObjects * 6 + 3) * 2 Then
                    'reset all
                    bufferRx = ""
                    nbOfObjects = data(2)
                    bufferRx = bc.HexFromBytes(data)
                    dataState = 1
                    counter = counter + 1
                Else If dataState = 1 Then
                    bufferRx = bufferRx & bc.HexFromBytes(data)
                    If bufferRx.Length = (nbOfObjects * 6 + 3) * 2 Then
                        dataState = 2
                        Log("dataState is " & dataState)
                        dataRx = bufferRx
                        tmp = dataRx.Length
                        Log("Before csv update " & dataRx.Length)
                        csvFileUpdate
                    End If
                End If
                   
                If counter > 5 Then
                    'reset buffer
                    counter = 0
                    dataState = 0  
                    Log("Need to reset message")  
                End If
                rxMsg.Text = rxMsg.Text & bc.HexFromBytes(data) & CRLF
            End If  
        Next
    End If
         
End Sub

This is the code for Android

B4X:
Sub bleRxManager_DataAvailable (Service As String, Characteristics As Map)
    Dim data() As Byte
       
    Sleep(0)
   
    If Not(notifyEnable) Then
        If Service.ToUpperCase = bleServiceUUI Then
            notifyEnable = True
            Sleep(2000)
            bleRxManager.SetNotify (bleServiceUUI.ToLowerCase, bleRxUUID.ToLowerCase, True)
                       
            startListening.Text =  "Stop Listening"
        End If
    Else
        For Each id As String In Characteristics.Keys
            If id = bleRxUUID.ToLowerCase Then
                'Log(Service)
                'Log(id)
                data = Characteristics.Get(id)
               
                Log(bc.HexFromBytes(data))
                Log("dataState is " & dataState)
                               
                If data(0) = 0x55 And Bit.And(0xFF, data(1)) = 0xFF And dataState = 0 Then
                    'start recording data Until Next start frame
                    counter = 0
                    Log("CURRENT COUNTER = " & counter)
                    bufferRx = ""
                    nbOfObjects = data(2)
                    bufferRx = bc.HexFromBytes(data)
                    dataState = 1
                Else If data(0) = 0x55 And Bit.And(0xFF, data(1)) = 0xFF And dataState = 1 And bufferRx.Length > (nbOfObjects * 6 + 3) * 2 Then
                    bufferRx = ""
                    nbOfObjects = data(2)
                    bufferRx = bc.HexFromBytes(data)
                    dataState = 1
                Else If dataState = 1 Then
                    bufferRx = bufferRx & bc.HexFromBytes(data)
                    counter = counter + 1
                    Log("CURRENT COUNTER = " & counter)
                    If bufferRx.Length = (nbOfObjects * 6 + 3) * 2 Then
                       
                        dataState = 2
                        Log("dataState is " & dataState)
                        dataRx = bufferRx
                        tmp = dataRx.Length
                        Log("Before csv update " & dataRx.Length)
                        csvFileUpdate
                        'dataState = False
                    End If
                End If
                                                   
                rxMsg.Text = rxMsg.Text & bc.HexFromBytes(data) & CRLF
                'csvFile.Add(bc.HexFromBytes(data))
            End If
        Next
    End If
           
End Sub
 
Upvote 0

alizeti

Member
Licensed User
It can become pretty big because it's all the message receive since connection.

the bluetooth module send around 246 bytes every 200ms.
On the iphone 6 and more, there is no problem. Yes it lags a bit but never missed a message!

On iphone 5s, after around 1500 message, it start to ack much slower and then at 1 point, the app does not respond anymore.
On android, ouf, after a couple of message it`s starts to freeze and the stop responding.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
That also looks like a loop that needs a good bit of processing power. I try to avoid those.

In case you cant, throw a Sleep(1) in there or something so it gives time for the device/UI to catch up.
 
Upvote 0

alizeti

Member
Licensed User
The purpose of this is to display a bunch of info coming from a board to the user.
The user know is speed, power, battery level, etc and even the possibility to save the data into a csv file.
There is also graphics to have live data
 
Upvote 0
Top