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?
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