I’m trying to send data from the Arduino to mobile app and I’m getting a wired result and I’m unsure why I will try and send HelloWord from the Arduino and it comes out on the B4A Log as the following
>H
>elloWorld
I have added the code I’m using below I got the code from the demo Bluetooth message app I have change the AStream.Initialize so its non-prefix, I’ve also change the code in the demo Bluetooth chat app so I can test it on the phone and it outputting the message on 2 line like the result above.
BluetoothManager
B4X:
AStream.Initialize(serial.InputStream, serial.OutputStream, “astream”)
Private Sub AStream_NewData (Buffer() As Byte)
CallSub2(SettingHome, “NewMessage”, BytesToString(Buffer, 0, Buffer.Length, ”UTF8”))
End Sub
SettingHome
B4X:
Public Sub NewMessage (msg As String)
Log(msg)
End Sub
Arduino Code
B4X:
bluetooth.print("HelloWorld");
i’m sending HelloWorld and its is not coming across complete on the mobile app
It is not unusual to have broken up messages via Bluetooth or WiFi. You need to either use prefix mode (if both sides of the conversation are using B4A) or use whatever message delimiter the other side is using (often a carriage return and/or line feed) to patch together the various bits of messages.
Something like this:
B4X:
Sub Globals
Dim RxBuffer as string
End Sub
Public Sub NewMessage (msg As String)
Dim i as int
'Log(msg)
RxBuffer = RxBuffer & msg
i = RxBuffer.IndexOf( CRLF )
If i > -1 then
Log( RxBuffer.SubString2( 0, i ))
RxBuffer = ""
End If
End Sub
It is not unusual to have broken up messages via Bluetooth or WiFi. You need to either use prefix mode (if both sides of the conversation are using B4A) or use whatever message delimiter the other side is using (often a carriage return and/or line feed) to patch together the various bits of messages.
I have added an example to my answer. I have not tried it but it should get you close. Replace the CRLF with whatever the Arduino is using for message termination, or add the CRLF yourself.
I have added an example to my answer. I have not tr4ied it but it should get you close. Replace the CRLF with whatever the Arduino is using for message termination, or add the CRLF yourself.
This example assumes that CRLF is used for message delimiter. If that's not the case, it will not work. Single step through the NewMessage() routine to find if any delimiter is used. You may have to add the CRLF yourself at the end of the message in the Arduino code
Is there away to get this to work without out using B4R as i have ready developed the Arduino side and now i would not be able to meet my deadline if i need to rewrite the code in B4R?
It is not unusual to have broken up messages via Bluetooth or WiFi. You need to either use prefix mode (if both sides of the conversation are using B4A) or use whatever message delimiter the other side is using (often a carriage return and/or line feed) to patch together the various bits of messages.
Something like this:
B4X:
Sub Globals
Dim RxBuffer as string
End Sub
Public Sub NewMessage (msg As String)
Dim i as int
'Log(msg)
RxBuffer = RxBuffer & msg
i = RxBuffer.IndexOf( CRLF )
If i > -1 then
Log( RxBuffer.SubString2( 0, i ))
RxBuffer = ""
End If
End Sub
There is a very small issue with that code, probably unlikely to happen... but just in case, consider this one-line change:
B4X:
If i > -1 then
Log( RxBuffer.SubString2( 0, i ))
'''RxBuffer = "" 'this will discard anything after the line terminator, eg (part of) the next line
RxBuffer = RxBuffer.SubString(i + 1) 'keep remainder of string after line terminator
End If
There is another small issue; again, it might not happen, but I'm watching Air Crash Investigations at the moment, and in a better-safe-than-sorry frame of mind:
B4X:
Public Sub NewMessage (Msg As String)
Dim I As Int
'''Log(Msg)
RxBuffer = RxBuffer & Msg
Do While True
I = RxBuffer.IndexOf(CRLF) 'CRLF is one character, not two... I kid you not... but works in our favour soon
If I < 0 Then
Exit
End If
Dim WholeLine As String = RxBuffer.SubString2(0, I) 'or I - 1 if you don't want the line terminator
RxBuffer = RxBuffer.SubString(I + 1) 'remove line from buffer
Log(WholeLine)
'''HandleLine(WholeLine) 'and/or deliver to your incoming text line handler
Loop
End Sub
The issue was that: only one text line was handled per NewMessage event. Thus, if the Arduino sends multiple lines per Msg, then the excess lines will slowly build up in RxBuffer and be handled late or possibly even never.