Hello,
we have developed device that sends HEX data in ASCII format via Bluetooth.
Now I have received data with B4A, which the device theoretically can not send at all. These are control characters like NUL, ACK, STX, and also special characters like @.
To be on the safe side, I have included the possibility to save the data from the serial buffer directly.
My questions: can the error with the invalid characters be due to the conversion with the ByteConverter or the UTF-8 encoding?
And am I doing the direct save with the RandomAccessFile correctly? I really want to save only the data that arrives from the Bluetooth device.
Thanks a lot in advance.
Here is an shortened snippet of the code:
we have developed device that sends HEX data in ASCII format via Bluetooth.
Now I have received data with B4A, which the device theoretically can not send at all. These are control characters like NUL, ACK, STX, and also special characters like @.
To be on the safe side, I have included the possibility to save the data from the serial buffer directly.
My questions: can the error with the invalid characters be due to the conversion with the ByteConverter or the UTF-8 encoding?
And am I doing the direct save with the RandomAccessFile correctly? I really want to save only the data that arrives from the Bluetooth device.
Thanks a lot in advance.
Here is an shortened snippet of the code:
B4X:
Private w3Raf As RandomAccessFile
Private BStreams As AsyncStreams
Private w3Init As Boolean
Private Serial1 As Serial
Public btdevice1 as string
Sub Service_Create
Serial1.Disconnect
Serial1.Initialize("Serial1")
End Sub
Sub Service_Start (StartingIntent As Intent)
Dim pairedDevices As Map = Serial1.GetPairedDevices
' Searching Bluetooth Device
'...
Serial1.Connect(btdevice1)
End Sub
Sub Serial1_Connected (Success As Boolean)
If Not(Success) Then Return
If BStreams.IsInitialized Then BStreams.Close
BStreams.Initialize(Serial1.InputStream, Serial1.OutputStream, "BStreams")
End Sub
Sub BStreams_NewData (Buffer() As Byte)
If writeLog Then
' Saving Buffer directly
If Not(w3Init) Then
w3Raf.Initialize(RawPath, "BtRAW.txt", False)
w3Init = True
End If
w3Raf.WriteBytes(Buffer, 0, Buffer.Length, w3Raf.CurrentPosition)
End If
' Processing Buffer
Dim bs As ByteConverter
Dim cominput As String
cominput = bs.StringFromBytes(Buffer, "UTF-8")
'...
End Sub
Sub Service_Destroy
If BStreams.IsInitialized Then BStreams.Close
If Serial1.IsInitialized Then Serial1.Disconnect
If w3Init Then
w3Raf.Close
w3Init = False
End If
End Sub