Android Question Issue with reading data from arduino via Bluetooth

Discussion in 'Android Questions' started by Ricky, Jul 30, 2019.

  1. Ricky

    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
    Code:
    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

    Code:
    Public Sub NewMessage (msg As String)
         
    Log(msg)
    End Sub
    Arduino Code
    Code:
    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
     
  2. Didier9

    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:

    Code:
    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: Jul 30, 2019
    Ricky likes this.
  3. Ricky

    Ricky Member Licensed User

    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.
     
  4. Didier9

    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: Jul 30, 2019
  5. Ricky

    Ricky Member Licensed User

    I've just tried the code and i'm not getting anything back in the log
     
  6. Didier9

    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
     
  7. Erel

    Erel Administrator Staff Member Licensed User

  8. Ricky

    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
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    No. As @Didier9 wrote you can make each message end with CRLF (printLine) and then use AsyncStreamsText to properly handle the incoming messages.
     
  10. thetahsk

    thetahsk Active Member Licensed User

  11. Erel

    Erel Administrator Staff Member Licensed User

    B4RSerializator by itself will not solve the problem where messages are split or merged.
     
  12. Ricky

    Ricky Member Licensed User

    Thanks Didier9 that works great :)
     
  13. emexes

    emexes Well-Known Member Licensed User

    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:
    Code:
    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
     
    Shelby and José J. Aguilar like this.
  14. emexes

    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:
    Code:
    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: Aug 1, 2019
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice