Android Question Issue with reading data from arduino via Bluetooth

Ricky

Member
Licensed User
Hi

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

Log Results from B4A

>H
>elloWorld


Thanks,
Ricky
 

Didier9

Active Member
Licensed User
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
 
Last edited:

Ricky

Member
Licensed User
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.
Because i'm not conversation on both sides are not using B4A that the reason i'm not using prefix mode.

In Arduino i've tired using
bluetooth.print("HelloWorld");
and
bluetooth.write("HelloWorld");

just in case that was causing a issue and i was getting the same result.
 

Didier9

Active Member
Licensed User
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.
 
Last edited:

Ricky

Member
Licensed User
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.
I've just tried the code and i'm not getting anything back in the log
 

Didier9

Active Member
Licensed User
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
 

Erel

Administrator
Staff member
Licensed User

Ricky

Member
Licensed User
Hi Erel

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?

Thanks,
Ricky
 

Ricky

Member
Licensed User
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
Thanks Didier9 that works great :)
 

emexes

Well-Known Member
Licensed User
Thanks Didier9 that works great :)
Ripper! I thought we'd lost you :-/

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
 

emexes

Well-Known Member
Licensed User
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.
 
Last edited:
Top