Android Question HC-06 send data to mobile and it lost data

dquy05

Member
Hi every one !

I make a app about HC-06 module bluetooth and mobile, but when HC-06 send data to mobile, it lost data, exactly is lost Buffer(0)
Ex : I send "12345", but receiver is "2345", can you help me? Thanks

B4X:
Private Sub Serial_Connected (Success As Boolean)
    ConnectionState = Success
    If Success = False Then
        Log(LastException.Message)
        ToastMessageShow("Error connecting: " & LastException.Message, True)
    Else
        If AStream.IsInitialized Then AStream.Close
        AStream.Initialize(serial.InputStream, serial.OutputStream, "AStream")
    End If
End Sub

Private Sub AStream_NewData (Buffer() As Byte)
    CallSub2(Main, "NewMessage", BytesToString(Buffer, 0, Buffer.Length, "Windows-1252"))
End Sub
 

dquy05

Member
Thank Mr.Erel !

I changed by ASCII and UTF8 but still lost data, exactly is lost Buffer(0), only receiv "2345"

B4X:
Private Sub Serial_Connected (Success As Boolean)
    ConnectionState = Success
    If Success = False Then
        Log(LastException.Message)
        ToastMessageShow("Error connecting: " & LastException.Message, True)
    Else
        If AStream.IsInitialized Then AStream.Close
        AStream.Initialize(serial.InputStream, serial.OutputStream, "AStream")
    End If
End Sub

Public Sub SendMessage (msg As String)
    AStream.Write(msg.GetBytes("UTF8"))
End Sub

Private Sub AStream_NewData (Buffer() As Byte)
    CallSub2(Main, "NewMessage", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

When I send by command, mobile receive enough "12345"

B4X:
Private Sub AStream_NewData (Buffer() As Byte)
    CallSub2(Main, "NewMessage", "12345")
End Sub
 
Upvote 0

dquy05

Member
Thanks Mr.Erel

I try it and result's OK, but I used sleep(100) and sb.Initialize, because I don't know when command CallSub2 finish? Can you help me?

B4X:
Private Sub AStream_NewData (Buffer() As Byte)
    sb.Append(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
    Dim s As String = sb.ToString   
    CallSub2(Main, "NewMessage", s)
    Sleep(100)   ' delay 100ms wait for sb and callsub are finish
    sb.Initialize  ' reinitialize
End Sub

I want to question about variable (Process_Globals type), when I creat it in Sub Process_Globals, but not found it if I call it in Class module

B4X:
Sub Process_Globals
    Private example As Int  ' this variable not found if I call it in Class module
End Sub

Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
I try it and result's OK, but I used sleep(100) and sb.Initialize, because I don't know when command CallSub2 finish? Can you help me?
CallSub, unlike CallSubDelayed, is synchronous. You don't need to add Sleep there.

I want to question about variable (Process_Globals type), when I creat it in Sub Process_Globals, but not found it if I call it in Class module
Make the variable a Public variable and access it with Main.example.

Please start a new thread for other questions.
 
Upvote 0

dquy05

Member
Hi Mr.Erel

CallSub, unlike CallSubDelayed, is synchronous. You don't need to add Sleep there.

HC-06 send to mobile string "12345", If don't use sleep(100) and sb.initialize then result strings receiv is "2345", and is use it, result is "12345"
I don't understand why?

Thank so much !
 
Upvote 0

dquy05

Member
My guess is that you have a problem in your Arduino code. Do you also encounter this issue with the B4A + B4R example?

Thanks for Mr.Erel

It work ok, because I don't use characters in finish, so when have interrupt always insert new characters, will lost received data
 
Upvote 0
Top