Main
#Region Project Attributes
#ApplicationLabel: BLE Teste New
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#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.
End Sub
Sub Globals
Private btnReadData As Button
Private btnDisconnect As Button
Private btnScan As Button
Private lblDeviceStatus As Label
Private lblState As Label
Private pbReadData As ProgressBar
Private pbScan As ProgressBar
Private clv As CustomListView
Private btEnviar As Button
Private Label3 As Label
Private lblDeviceMessage As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
End Sub
Sub Activity_Resume
StateChanged
End Sub
Public Sub StateChanged
lblState.Text = Starter.currentStateText
If Starter.connected Then
lblDeviceStatus.Text = "Connected: " & Starter.ConnectedName
Else
lblDeviceStatus.Text = "Not connected"
End If
btnDisconnect.Enabled = Starter.connected
btnScan.Enabled = Not(Starter.connected)
pbReadData.Visible = False
pbScan.Visible = False
btnReadData.Enabled = Starter.connected
btnScan.Enabled = (Starter.currentState = Starter.manager.STATE_POWERED_ON) And Starter.connected = False
End Sub
Public Sub QualID
Label3.Text = "X"
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub btnScan_Click
Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_COARSE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then Return
pbScan.Visible = True
CallSub(Starter, "StartScan")
End Sub
Sub DataAvailable (Service As String, Characteristics As Map)
pbReadData.Visible = False
clv.Add(CreateServiceItem(Service), "")
For Each id As String In Characteristics.Keys
clv.Add(CreateCharacteristicItem(id, Characteristics.Get(id)), "")
Next
End Sub
Sub btnDisconnect_Click
CallSub(Starter, "Disconnect")
End Sub
Sub btnReadData_Click
pbReadData.Visible = True
clv.Clear
CallSub(Starter, "ReadData")
End Sub
Sub CreateServiceItem (service As String) As Panel
Dim pnl As Panel
pnl.Initialize("")
pnl.Color = 0xFF808080
pnl.SetLayoutAnimated(0, 0, 0, clv.AsView.Width, 30dip)
Dim lbl As Label
lbl.Initialize("")
lbl.Text = service
lbl.Gravity = Gravity.CENTER
lbl.Typeface = Typeface.DEFAULT_BOLD
pnl.AddView(lbl, 0, 0, clv.AsView.Width, 30dip)
Return pnl
End Sub
Sub CreateCharacteristicItem(Id As String, Data() As Byte) As Panel
Dim pnl As Panel
pnl.Initialize("")
pnl.SetLayoutAnimated(0, 0, 0, clv.AsView.Width, 40dip)
pnl.Color = Colors.White
Dim lbl As Label
lbl.Initialize("")
lbl.Text = Id
pnl.AddView(lbl, 0, 0, clv.AsView.Width, 20dip)
Dim lbl2 As Label
lbl2.Initialize("")
Try
lbl2.Text = BytesToString(Data, 0, Data.Length, "UTF8")
Catch
Log(LastException)
lbl2.Text = "Error reading data as string"
End Try
lbl2.TextColor = 0xFF909090
lbl2.TextSize = 14
pnl.AddView(lbl2, 0, 20dip, clv.AsView.Width, 20dip)
Return pnl
End Sub
Sub btEnviar_Click
CallSub(Starter, "WriteData")
End Sub
Public Sub DeviceMessage (s As String)
lblDeviceMessage.Text = "Device message: " & s
End Sub