Android Question Bluetooth Communication error!

Amir7741

New Member
Hello guys
I'm very beginner on B4A .
im try to communication between my phone (huawei G-700) and on serial <-> bluetooth module (HC05)
i have one problem when receiving data. the phone connect to BT module , but when receive data happen ,all things come to wrong , may phone's bluetooth isnt responding to anythings. need to restart the phone.
Help me PLS


B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
    If FirstTime Then
        ser.Initialize("Serial1")
        Timer1.Initialize("Timer1", 300)
    End If
End Sub

Sub Activity_Resume
    If ser.IsEnabled = False Then
        Msgbox("Please enable Bluetooth.", "")
    Else
        ser.Listen 'listen for incoming connections
    End If
End Sub


B4X:
Sub Timer1_Tick

Dim in_str As String
If connected Then
   If TextReader1.Ready Then
      in_str=TextReader1.ReadLine
     Do While in_str <> Null
        Label1.Text=in_str
        in_str=TextReader1.ReadLine
     Loop
   
   End If
End If
End Sub
 

Amir7741

New Member
You should use AsyncStreams.
Hi Erel
last night i was test Async too.but whenever phone want to connect to BT Module , connection broken happen :(
pls Help me

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim ser As Serial
    'Dim TextReader1 As TextReader
    'Dim TextWriter1 As TextWriter
    Dim Timer1 As Timer
    Dim connected As Boolean
    Dim time_out As Int
    Dim AStream As AsyncStreams
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Label1 As Label
    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private txtLog As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
    If FirstTime Then
        ser.Initialize("Serial1")
        'Timer1.Initialize("Timer1", 300)
    End If

End Sub

Sub AStream_NewData (Buffer() As Byte)
    LogMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub AStream_Error
    ToastMessageShow("Connection is broken.", True)
    'btnSend.Enabled = False
    'txtInput.Enabled = False
End Sub

Sub AStream_Terminated
    AStream_Error
End Sub

Sub LogMessage(From As String, Msg As String)
    txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
    txtLog.SelectionStart = txtLog.Text.Length
End Sub
Sub Activity_Resume
    If ser.IsEnabled = False Then
        Msgbox("Please enable Bluetooth.", "")
    Else
        ser.Listen 'listen for incoming connections
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
If connected Then
   ser.Disconnect
End If
End Sub



Sub Button1_Click
    Dim PairedDevices As Map
    PairedDevices = ser.GetPairedDevices
  
    Dim l As List
    l.Initialize
    For i = 0 To PairedDevices.Size - 1
        l.Add(PairedDevices.GetKeyAt(i))
    Next
    Dim res As Int
    res = InputList(l, "Choose device", -1) 'show list with paired devices
    If res <> DialogResponse.CANCEL Then
        'Msgbox(l.Get(res),"Microlino")
        ser.Connect(PairedDevices.Get(l.Get(res))) 'convert the name to mac address
      
    End If  
End Sub

Sub Serial1_Connected (Success As Boolean)
    If Success Then
        Label1.Text="Connected successfully"
        'TextReader1.Initialize(ser.InputStream)
        'TextWriter1.Initialize(ser.OutputStream)

        'If AStream.IsInitialized = False Then
            AStream.InitializePrefix(ser.InputStream, True, ser.OutputStream, "AStream")
        'End If
        'Timer1.Enabled = True
        connected = True
        'TextWriter1.WriteLine("$")
        'TextWriter1.Flush
    Else
        connected = False
        'Timer1.Enabled = False
        Msgbox(LastException.Message, "Error connecting.")
    End If
End Sub
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
You should use HC-06
HC-05 is host and your device's Bluetooth is also host, so they can't talk to each other.
 
Upvote 0
Top