Android Question Bluetooth and HC-06

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

Similar threads

B4A Code Snippet [B4X] GUID
B4A Question RN41 and a PIC Microcontroller
B4A Tutorial Android Serial tutorial
B4A Question RadioButton Event ?
B4A Tutorial Text files
  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 (
    #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)
    If AStream.IsInitialized = False Then
            AStream.Initialize (Main.serial1.InputStream ,Main.serial1 .OutputStream ,
    End If
        txtLog.Width = 
    End Sub

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

    End Sub

    Sub Activity_Resume
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
    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 (
    "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

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