Hello:
I"m trying to develop an application to control remote devices from smartphone (that's working) and also to read measurements from sensors: this isn't working.
when I send data from PC (emulating hardware) to smartphone, the application blocked and stopped. I get the snippet from the forum and modified it to my needs
any help is appreciated
I"m trying to develop an application to control remote devices from smartphone (that's working) and also to read measurements from sensors: this isn't working.
when I send data from PC (emulating hardware) to smartphone, the application blocked and stopped. I get the snippet from the forum and modified it to my needs
any help is appreciated
B4X:
#Region Project Attributes
#ApplicationLabel: bluetooth1
#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
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Serial1 As Serial
Dim TextReader1 As TextReader
Dim TextWriter1 As TextWriter
Dim Timer1 As Timer
Dim connected As Boolean
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.
Dim txtLog As EditText
Dim Dev1 As Button
Dim Dev2 As Button
Dim Dev3 As Button
Dim Dev4 As Button
Dim Dev5 As Button
Dim Dev6 As Button
Dim Dev7 As Button
Dim Dev8 As Button
Dim Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
Serial1.Initialize("Serial1")
Timer1.Initialize("Timer1", 200)
End If
Activity.LoadLayout("layout1")
Activity.AddMenuItem("Connect", "mnuConnect")
Activity.AddMenuItem("Disconnect", "mnuDisconnect")
txtLog.Text=""
End Sub
Sub Activity_Resume
If Serial1.IsEnabled = False Then
Msgbox("Please enable Bluetooth.", "")
Else
Serial1.Listen 'listen for incoming connections
End If
End Sub
Sub mnuConnect_Click
Dim PairedDevs As Map
PairedDevs = Serial1.GetPairedDevices
Dim l As List
l.Initialize
For i = 0 To PairedDevs.Size - 1
l.Add(PairedDevs.GetKeyAt(i))
Next
Dim res As Int
res = InputList(l, "Choose Dev", -1) 'show list with paired Devs
If res <> DialogResponse.CANCEL Then
Serial1.Connect(PairedDevs.Get(l.Get(res))) 'convert the name to mac address
End If
End Sub
Sub Serial1_Connected (Success As Boolean)
If Success Then
ToastMessageShow("Connected successfully", False)
TextReader1.Initialize(Serial1.InputStream)
TextWriter1.Initialize(Serial1.OutputStream)
Timer1.Enabled = True
connected = True
Else
connected = False
Timer1.Enabled = False
Msgbox(LastException.Message, "Error connecting.")
End If
End Sub
Sub mnuDisconnect_Click
Serial1.Disconnect
connected = False
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Dev1_Click
If connected Then
TextWriter1.Write("A")
TextWriter1.Flush
End If
End Sub
Sub Dev2_Click
If connected Then
TextWriter1.Write("B")
TextWriter1.Flush
End If
End Sub
Sub Dev3_Click
If connected Then
TextWriter1.Write("C")
TextWriter1.Flush
End If
End Sub
Sub Dev4_Click
If connected Then
TextWriter1.Write("D")
TextWriter1.Flush
End If
End Sub
Sub Dev5_Click
If connected Then
TextWriter1.Write("a")
TextWriter1.Flush
End If
End Sub
Sub Dev6_Click
If connected Then
TextWriter1.Write("b")
TextWriter1.Flush
End If
End Sub
Sub Dev7_Click
If connected Then
TextWriter1.Write("c")
TextWriter1.Flush
End If
End Sub
Sub Dev8_Click
If connected Then
TextWriter1.Write("d")
TextWriter1.Flush
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