Android Question Bluetooth reception problem

Similar threads

B4A Tutorial Text files
B4A Tutorial Android Serial tutorial
B4A Question Split data on WriteLine
B4A Question Problem with Textwriter
B4A Question TextWriter Not creating a file

IslamQabel

Active Member
Licensed User
Dear All:
I made a code for bluetooth sending/receiving, the code was working perfect in the past for around two years ago.
i did not use B4A for along time, and uninstalled it due to windows problems.
since i did not update B4A, i am still using B4A version 4.3.
when i complied the code(see attached code), no problem, installed OK, sending process by bluetooth is ok
but when i receive anything from a bluetooth module, i can see clearly what i sent correctly in "txtlog" then the app is hanging out , nothing works in the app....i do not know why? although that code was working perfectly with me.
i think the source of problem is "timer1_tick" because when i disable Timer1, no problem of hanging out except i can not receive anything....but i am still unable to solve that problem


B4X:
Sub Timer1_Tick
    If connected Then
        If TextReader1.Ready Then 'check if there is any data waiting to be read
            txtLog.Text = txtLog.Text & TextReader1.ReadLine & CRLF
            txtLog.SelectionStart = txtLog.Text.Length
          
          
        End If
    End If
End Sub
could you please help me?

the code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: BlueSR2
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim admin As BluetoothAdmin
    Dim serial1 As Serial
    Dim foundDevices As List
    Type NameAndMac (Name As String, Mac As String)
    Dim connectedDevice As NameAndMac
    Dim TextWriter1 As TextWriter
    Dim connected As Boolean
    Dim TextReader1 As TextReader
    Dim Timer1 As Timer
  
End Sub

Sub Globals
    '******************************************************
  
    Private Pnltest As Panel
    Private scvTest As HorizontalScrollView
    Private BtnA As Button
    Private BtnB As Button
    Private BtnC As Button
    Private BtnD As Button
    Private BtnE As Button
    Private BtnF As Button
    Private BtnG As Button
    Private BtnH As Button
    Private BtnI As Button
    Private BtnJ As Button
    Private Btnplus2 As Button
    Private Btnminus2 As Button
    Private BtnU As Button
    Private BtnL As Button
    Private BtnS As Button
    Private BtnR As Button
    Private BtnDD As Button
    Private Btnplus1 As Button
    Private Label1 As Label
    Private txtLog As EditText
    Private txtSend As EditText
    Private BtnSend As Button
    Private Label2 As Label
    Private Label3 As Label
    Private Btnminus1 As Button
   
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    '***************************************************
    Activity.LoadLayout("1")
    scvTest.Panel.LoadLayout("ScrollViewLayout")
    scvTest.Panel.Width=Pnltest.Width
    '***************************************************
    If FirstTime Then
        admin.Initialize("admin")
        serial1.Initialize("serial1")
        Timer1.Initialize("Timer1",200)
    End If
    Activity.AddMenuItem("TurnOnBT","MenuTurnOn")
    Activity.AddMenuItem("Search&Connect","MenuConnect")
    Activity.AddMenuItem("Connect to Paired Device","MenuPaired")
    Activity.AddMenuItem("Disconnect","MenuDisconnect")
   
    Activity.AddMenuItem("HELP","MenuHelp")
  
   
End Sub

Sub Activity_Resume
   '****************************************************************************
    If admin.IsEnabled = False Then
        If admin.Enable = False Then
            ToastMessageShow("Bluetooth is not enabled,Please Press menu and click on Connect&Search.", True)
        Else
            ToastMessageShow("Bluetooth is Enabled, Press Menu and click on Connect&Search...", True)
           
        End If
    End If
    '***************************************************************************
  
End Sub

Sub Activity_Pause (UserClosed As Boolean)
'*******************************************************
   If UserClosed=True Then
         serial1.Disconnect
    
     End If

'*******************************************************
End Sub

Sub MenuTurnOn_Click
    'this intent makes the device discoverable for 200 seconds.
    Dim i As Intent
    i.Initialize("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", "")
    i.PutExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 200)
    StartActivity(i)
End Sub
Sub MenuConnect_click
    foundDevices.Initialize
    If admin.StartDiscovery    = False Then
        ToastMessageShow("Error starting discovery process.", True)
    Else
        ProgressDialogShow("please wait,Searching for devices...")
    End If
End Sub
Sub Admin_DiscoveryFinished
    ProgressDialogHide
    If foundDevices.Size = 0 Then
        ToastMessageShow("No device found.", True)
    Else
        Dim l As List
        l.Initialize
        For i = 0 To foundDevices.Size - 1
            Dim nm As NameAndMac
            nm = foundDevices.Get(i)
            l.Add(nm.Name)
        Next
        Dim res As Int
        res = InputList(l, "Choose device to connect", -1)
        If res <> DialogResponse.CANCEL Then
            connectedDevice = foundDevices.Get(res)
            ProgressDialogShow("Connecting to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
            serial1.Connect(connectedDevice.Mac)
        End If
    End If
End Sub
Sub Admin_DeviceFound (Name As String, MacAddress As String)
    Log(Name & ":" & MacAddress)
    Dim nm As NameAndMac
    nm.Name = Name
    nm.Mac = MacAddress
    foundDevices.Add(nm)
    ProgressDialogShow("Searching for devices (~ device found)...".Replace("~", foundDevices.Size))
End Sub
Sub MenuPaired_Click
    Dim PairedDevices As Map
    PairedDevices = serial1.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 to connect", -1) 'show list with paired devices
    If res <> DialogResponse.CANCEL Then
        serial1.Connect(PairedDevices.Get(l.Get(res))) 'convert the name to mac address
    End If
End Sub
Sub Serial1_Connected (Success As Boolean)
    ProgressDialogHide
    ToastMessageShow("Connected Successfully", True)
    TextWriter1.Initialize(serial1.OutputStream)
    TextReader1.Initialize(serial1.InputStream)
    Timer1.Enabled=True
    connected = True
    If Success = False Then
        Log(LastException.Message)
        ToastMessageShow("Error connecting: " & LastException.Message, True)
   
    End If
End Sub
Sub BtnB_Click
    If connected Then
        TextWriter1.WriteLine(BtnB.Tag)
        TextWriter1.Flush
   
    End If
End Sub
Sub BtnA_Click
    If connected Then
        TextWriter1.WriteLine(BtnA.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub BtnJ_Click
    If connected Then
        TextWriter1.WriteLine(BtnJ.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub BtnI_Click
    If connected Then
        TextWriter1.WriteLine(BtnI.Tag)
        TextWriter1.Flush
   
    End If
End Sub
Sub BtnH_Click
    If connected Then
        TextWriter1.WriteLine(BtnH.Tag)
        TextWriter1.Flush
   
    End If
End Sub
Sub BtnG_Click
    If connected Then
        TextWriter1.WriteLine(BtnG.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub BtnF_Click
    If connected Then
        TextWriter1.WriteLine(BtnF.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub BtnE_Click
    If connected Then
        TextWriter1.WriteLine(BtnE.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub BtnD_Click
    If connected Then
        TextWriter1.WriteLine(BtnD.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub BtnC_Click
    If connected Then
        TextWriter1.WriteLine(BtnC.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub BtnU_Click
    If connected Then
        TextWriter1.WriteLine(BtnU.Tag)
        TextWriter1.Flush
       
    End If
End Sub

Sub BtnR_Click
    If connected Then
        TextWriter1.WriteLine(BtnR.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub BtnS_Click
    If connected Then
        TextWriter1.WriteLine(BtnS.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub BtnL_Click
    If connected Then
        TextWriter1.WriteLine(BtnL.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub BtnDD_Click
    If connected Then
        TextWriter1.WriteLine(BtnDD.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub btnplus1_Click
    If connected Then
        TextWriter1.WriteLine(Btnplus1.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub Btnminus1_Click
    If connected Then
        TextWriter1.WriteLine(Btnminus1.Tag)
        TextWriter1.Flush
   
    End If
End Sub
Sub Btnplus2_Click
    If connected Then
        TextWriter1.WriteLine(Btnplus2.Tag)
        TextWriter1.Flush
       
    End If
End Sub
Sub btnminus2_Click
    If connected Then
        TextWriter1.WriteLine(Btnminus2.Tag)
        TextWriter1.Flush
   
    End If
End Sub
Sub btnSend_Click
    If connected Then
        TextWriter1.WriteLine(txtSend.Text)
        TextWriter1.Flush
        txtSend.Text = ""
    End If
End Sub
Sub Timer1_Tick
    If connected Then
        If TextReader1.Ready Then 'check if there is any data waiting to be read
            txtLog.Text = txtLog.Text & TextReader1.ReadLine & CRLF
            txtLog.SelectionStart = txtLog.Text.Length
           
           
        End If
    End If
End Sub
Sub MenuDisconnect_Click
    serial1.Disconnect
    connected=False
   
End Sub
Sub MenuHelp_click
  
    Msgbox(File.ReadString(File.DirAssets, "test.txt"),"HELP")
End Sub
i can not attach all project file because it exceeds the limit of uploading files
 

IslamQabel

Active Member
Licensed User
unfortunately, the program did not work with my hardware successfully....first of all i am communicating with slave bluetooth module that is connected with UART module of micro-controller....

After installation the code, succeeded to communicate with the bluetooth module..

when sending characters , the hardware can not respond (the code i sent before , the hardware was responding).

when receiving any things sent by the slave bluetooth module, a message of error " connection is broken" although the bluetooth module led indicated that it still connected with the app.
i am still confused about the problem of error?
 

Erel

Administrator
Staff member
Licensed User
The example will not work with your hardware. It just shows how to use AsyncStreams. Make sure to initialize AsyncStreams in regular mode, not prefix mode.

i am still confused about the problem of error?
TextReader.ReadLine reads data until it finds an end of line character. If for some reason it doesn't find it then the program will freeze until the OS kills it.

It is never a good idea to implement network communication over the main thread. The result will be bad.
 

IslamQabel

Active Member
Licensed User
thanks for reply...
1) How i can initialize it in regular mode ? and if i do, does the program work with my hardware?
2) what is end of line character to send it with every command from my hardware? and if it finds the end of line character, the problem will be solved?
3) confusion was for the code you sent that it did not work with me...i understood a little about textreader.
 

IslamQabel

Active Member
Licensed User
Hi...Appreciate your help
Actually, the problem was completely solved by sending End of line character "which is 13 in ASCII code" as you told before ..
TextReader.ReadLine reads data until it finds an end of line character. If for some reason it doesn't find it then the program will freeze until the OS kills it.
So in my opinion, if someone prefer to use Textreader in serial receiving from UART module in micro-controllers send by bluetooth module connected ,they should be sure when sending any data it should be ended by end of line character to prevent program freeze
that end of line character gave me hope again:)
thanks
 

IslamQabel

Active Member
Licensed User
Another question...
as i mentioned before, i am receiving via UART module......when i send something via my application as shown in the following code.....

For sending numbers...which is correct and what is the difference?
B4X:
Sub BtnSnap_Click
    If connected Then
        TextWriter1.WriteLine(BtnSnap.Tag)
        TextWriter1.Flush
       BtnSnap.Tag = "20"
    End If
End Sub
code 2:
B4X:
Sub BtnSnap_Click
    If connected Then
        TextWriter1.WriteLine(BtnSnap.Tag)
        TextWriter1.Flush
       BtnSnap.Tag = 20
    End If
End Sub
All i want to do is sending numbers via my App and then when received, the hardware will convert it to 20
thanks
 

IslamQabel

Active Member
Licensed User
another something.....the value of number is random....i can entered it via Edit text object...so the button will capture the value from Edit Text or from any object
thanks
 
Last edited:
Top