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
could you please help me?
the code:
i can not attach all project file because it exceeds the limit of uploading files
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
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