Android Question Bluetooth and HC-06

Discussion in 'Android Questions' started by stari, Jul 17, 2015.

  1. stari

    stari Active Member Licensed User

    I made a connection from Android to PIC over HC-06. The problem is that I can not send more than 3 characters from my application. I have modified Erel-s Bluetooth tutorial (http://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/#content).
    Code:
    #Region Module Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
    #End Region

    'Activity module
    Sub Process_Globals
        
    Dim AStream As AsyncStreams
    End Sub

    Sub Globals
        
    Dim txtInput As EditText
        
    Dim txtLog As EditText
        
    Dim btnSend As Button
        
    Public charset As String = "UTF8"
        
    Private sb As StringBuilder
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("2")
        
    If AStream.IsInitialized = False Then
            AStream.Initialize (Main.serial1.InputStream ,Main.serial1 .OutputStream ,
    "AStream")
        
    End If
        sb.Initialize
        txtLog.Width = 
    100%x
    End Sub

    Private Sub astream_NewData (Buffer() As Byte)
       
        sb.Append(
    BytesToString(Buffer, 0, Buffer.Length, charset))
        
    Dim s As String = sb.ToString
       
        
    Log("S =  " & s)
        
    Log("SB = " & sb) 
       
        
    If s.Contains ("*"Then
            LogMessage(
    "PIC  ", s)
            sb.Initialize
            s = 
    ""
        
    End If

    End Sub


    Sub Activity_Resume
       
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
        
    If UserClosed Then
            AStream.Close
        
    End If
    End Sub

    Sub txtInput_EnterPressed
        
    If btnSend.Enabled = True Then btnSend_Click
    End Sub


    Sub btnSend_Click
        
    Dim out_mess As String
        out_mess = txtInput.Text & 
    "*"'& CRLF
        Log("Out Mess = " & out_mess)
        AStream.Write2 (out_mess.GetBytes (
    "UTF8"),0,out_mess.Length)
        txtInput.SelectAll
        txtInput.RequestFocus
        LogMessage(
    "Me", txtInput.Text)
    End Sub

    Sub LogMessage(From As String, Msg As String)
        txtLog.Text = txtLog.Text & From & 
    ": " & Msg & CRLF
        txtLog.SelectionStart = txtLog.Text.Length
    End Sub
    Here my code.
    If i type 123 in the txtInput box, then all 3 chars are transfered to PIC. If there are more chars, then only first 3 are transfered.
    Comunity, mybee is too hot for thinking but anyway,... thks for help.
     
  2. Roycefer

    Roycefer Well-Known Member Licensed User

    AStream.Write2() has a Boolean return value. You should capture that and inspect it to see if AStream.Write2() is able to enqueue the write request.

    I'm not familiar enough with the PIC to know if it's necessary but you might also try using AStream.InitializePrefix() instead of the just AStream.Initialize.
     
  3. stari

    stari Active Member Licensed User

    Thks. The HC-06 don't work in prefix mode. 3 chars are received by HC-05 in right format. The next chars are not sendet to HC-06. Now i will try to test AStream.Write2, as you say.
     
  4. stari

    stari Active Member Licensed User

    Done.
    Log ("Astream.Write2 " & AStream.Write2 (out_mess.GetBytes ("UTF8"),0,out_mess.Length))
    Log: upload_2015-7-17_17-46-41.png
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Your code looks correct. The problem is somewhere else. Maybe the device expects a windows end of line - Chr(13) & Chr(10) ?
     
  6. stari

    stari Active Member Licensed User

    Thks, Erel. Yes, problem solved. Error was on other, PIC, side.
     
Loading...