iOS Question Bluetooth-class

D

Deleted member 103

Guest
Hi,

I have a problem with a Bluetooth class that I can not solve alone. With this class I receive strings from an Arduino with BLE-Modul. Unfortunately the strings are not always correctly interpreted and the app leads the wrong code.
Often the code is executed as if the string "0" was read.
Important! The string length is not always the same.

For Arduino-Bluetooth-version-1, the string length is always 1.
For Arduino-Bluetooth-version-2, the string length is always 2 to 9.

The error must be while reading, but I do not know where the error is.
Can someone help me?

B4X:
Private Sub btManager_DataAvailable(Service As String, Characteristics As Map)
    Dim count As Int
    Dim key() As Byte
   
    key = Characteristics.Get("FFE1")
    KeyString = bc.StringFromBytes(key,"UTF8")
   
    KeyCode = ""

    Select KeyString.CharAt(0)
        Case "0", "+"
            'Bluetooth-Version 1
            KeyCode = KeyString.SubString2(0,1)
            KeyString = ""
        Case "2","3","4","5","6","7","8","9"
            'Bluetooth-Version 2
            If IsNumber(KeyString.SubString2(0,1) ) And KeyString.Length > 1 Then
                count = KeyString.SubString2(0,1)
                If KeyString.Length < count Then Return
                If KeyString.Length >= 2 Then KeyCode = KeyString.SubString2(1,2)
            End If
    End Select   

    Select KeyCode
        Case "0"
            Main.EvaluateSensorKey
        Case "+"
            setDistanceTraveled
           
            counter = counter + 1
            If counter = 1 Then
                'Ab den ersten Abrollumfang wird die Zeit notiert.
                starttime = DateTime.Now
            Else If counter >= StepCounter Then
                'Geschwindigkeitberechen immer nach 5x Abrollumfang
                TempDistance = counter * Main.flAbrollumfang
                speed = TempDistance / (DateTime.Now - starttime) * 3600
                counter = 0
               
                If speed < 25.0 Then
                    StepCounter = 4
                Else If speed < 50.0 Then
                    StepCounter = 7
                Else If speed < 75.0 Then
                    StepCounter = 10
                Else If speed < 100.0 Then
                    StepCounter = 14
                Else If speed < 125.0 Then
                    StepCounter = 18
                Else If speed < 150.0 Then
                    StepCounter = 22
                Else If speed < 175.0 Then
                    StepCounter = 26
                Else
                    StepCounter = 30
                End If
            End If
           
            last_starttime = DateTime.Now
        Case "*"
            setDistanceTraveled
            If IsNumber(KeyString.SubString(2)) Then
                speed = KeyString.SubString(2)
                last_starttime = DateTime.Now
            End If
        Case "v"
            Version = KeyString.SubString(2)
    End Select
    KeyString = ""
   
    If btManager.Tag = False Then
        btManager.SetNotify(ConnectedServices.Get(btService), "FFE1", True)
        btManager.Tag = True
        timerSpeed.Enabled = IsBtConnected       
    End If
End Sub
 
D

Deleted member 103

Guest
What is the output of:
B4X:
Log(bc.HexFromFromBytes(key))
B4X:
KeyString=20; Key(0)=50
bc.HexFromBytes=3230
KeyString=5*0.0; Key(0)=53
bc.HexFromBytes=352A302E30
KeyString=5*4.1; Key(0)=53
bc.HexFromBytes=352A342E31
KeyString=5*8.2; Key(0)=53
bc.HexFromBytes=352A382E32
KeyString=6*11.8; Key(0)=54
bc.HexFromBytes=362A31312E38
KeyString=6*15.6; Key(0)=54
bc.HexFromBytes=362A31352E36
KeyString=6*19.1; Key(0)=54
bc.HexFromBytes=362A31392E31
KeyString=6*19.4; Key(0)=54
bc.HexFromBytes=362A31392E34
KeyString=6*23.7; Key(0)=54
bc.HexFromBytes=362A32332E37
KeyString=6*22.0; Key(0)=54
bc.HexFromBytes=362A32322E30
KeyString=6*25.8; Key(0)=54
bc.HexFromBytes=362A32352E38
KeyString=6*34.2; Key(0)=54
bc.HexFromBytes=362A33342E32
KeyString=6*33.0; Key(0)=54
bc.HexFromBytes=362A33332E30
KeyString=6*30.4; Key(0)=54
bc.HexFromBytes=362A33302E34
KeyString=6*34.6; Key(0)=54
bc.HexFromBytes=362A33342E36
KeyString=6*32.7; Key(0)=54
bc.HexFromBytes=362A33322E37
KeyString=6*26.7; Key(0)=54
bc.HexFromBytes=362A32362E37
KeyString=6*29.8; Key(0)=54
bc.HexFromBytes=362A32392E38
KeyString=6*32.2; Key(0)=54
bc.HexFromBytes=362A33322E32
KeyString=6*34.3; Key(0)=54
bc.HexFromBytes=362A33342E33
KeyString=6*36.9; Key(0)=54
bc.HexFromBytes=362A33362E39
KeyString=6*33.3; Key(0)=54
bc.HexFromBytes=362A33332E33
KeyString=6*33.1; Key(0)=54
bc.HexFromBytes=362A33332E31
KeyString=6*31.4; Key(0)=54
bc.HexFromBytes=362A33312E34
KeyString=6*27.0; Key(0)=54
bc.HexFromBytes=362A32372E30
KeyString=6*21.8; Key(0)=54
bc.HexFromBytes=362A32312E38
KeyString=6*21.0; Key(0)=54
bc.HexFromBytes=362A32312E30
KeyString=6*16.6; Key(0)=54
bc.HexFromBytes=362A31362E36
KeyString=6*11.4; Key(0)=54
bc.HexFromBytes=362A31312E34
KeyString=5*8.7; Key(0)=53
bc.HexFromBytes=352A382E37
KeyString=5*6.5; Key(0)=53
bc.HexFromBytes=352A362E35
KeyString=5*5.9; Key(0)=53
bc.HexFromBytes=352A352E39
In principle is always correct, only occasionally there is an outlier and I do not know why. :(
The string " KeyString=5*4.1" means:
5 = String length
* = Value for speed comes
4.1 = Speed in km/h
 
Upvote 0
Top