#Region Project Attributes
#ApplicationLabel: B4A Example
#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
'should be private
End Sub
Sub Globals
Private btnDisconnect As Button
Private btnScan As Button
Private lblState As Label
Private pbScan As ProgressBar
Private clv As CustomListView
Private uiitemas As Map
Private xui As XUI
Private Print As Button
Private EmailBtn As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
uiitemas.Initialize
'i = 0
End Sub
Sub Activity_Resume
StateChanged
End Sub
Public Sub StateChanged
lblState.Text = Starter.currentStateText
btnDisconnect.Enabled = Starter.scanning
pbScan.Visible = Starter.scanning
btnScan.Enabled = (Starter.currentState = Starter.manager.STATE_POWERED_ON) And Starter.scanning = False
Dim beaconsToRemove As List
For Each beacon As Beacon In Starter.beacons.Values
If uiitemas.ContainsKey(beacon.uniqueid) = False Then
Dim p As Panel = CreateItem(beacon)
clv.Add(p, "")
uiitemas.Put(beacon.uniqueid, p)
Else
If beacon.time + Starter.TIME_TO_LIVE_SECONDS * DateTime.TicksPerSecond < DateTime.Now Then
'remove old beacon. We need to do it after the For Each loop.
If beaconsToRemove.IsInitialized = False Then beaconsToRemove.Initialize
beaconsToRemove.Add(beacon)
Else
Dim p As Panel = uiitemas.Get(beacon.uniqueid)
SetDistance(p.GetView(1), beacon)
End If
End If
Next
If beaconsToRemove.IsInitialized Then
For Each beacon As Beacon In beaconsToRemove
Dim p As Panel = uiitemas.Get(beacon.uniqueid)
clv.RemoveAt(clv.GetItemFromView(p))
uiitemas.Remove(beacon.uniqueid)
Starter.beacons.Remove(beacon.uniqueid)
Next
End If
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 Then
pbScan.Visible = True
CallSub(Starter, "Scan")
StateChanged
Else
ToastMessageShow("No permission...", True)
End If
End Sub
Sub btnDisconnect_Click
CallSub(Starter, "StopScan")
StateChanged
End Sub
Sub CreateItem(beacon As Beacon) As Panel
Dim pnl As B4XView = xui.CreatePanel("")
pnl.SetLayoutAnimated(0, 0, 0, clv.AsView.Width, 50dip)
pnl.LoadLayout("Item")
Dim FirstLine As String
Select beacon.BeaconType
Case Starter.Parser.TypeiBeacon
Dim ib As iBeacon = beacon.SpecificData
FirstLine = $"iBeacon: ${ib.uuid} (${ib.major}/${ib.minor})"$
Case Starter.Parser.TypeEddystoneUID
Dim euid As EddystoneUID = beacon.SpecificData
FirstLine = $"Eddystone UID: ${euid.Namespace}:${euid.Instance}"$
Case Starter.Parser.TypeEddystoneURL
Dim eurl As EddystoneURL = beacon.SpecificData
FirstLine = $"Eddystone URL: ${eurl.url}"$
End Select
pnl.GetView(0).Text = FirstLine
SetDistance(pnl.GetView(1), beacon)
Return pnl
End Sub
Private Sub SetDistance(lbl As Label, beacon As Beacon)
lbl.Text = $"Distance: $1.1{beacon.distance} meters"$
End Sub
Private Sub Print_Click
Dim List1 As List
List1.Initialize
For i = 0 To 9999
List1.Add((Starter.RSSIData(i) & "," &Starter.RSSITime(i)))
Next
If File.Exists(File.DirInternal, "RSSIdata.txt") Then
File.Delete(File.DirInternal, "RSSIdata.txt")
End If
File.WriteList(File.DirInternal, "RSSIdata.txt", List1)
End Sub