Sub ScanForDevices
Dim servicelist, devices As List
Dim sheet As ActionSheet
servicelist.Initialize
devices.Initialize
HD.ProgressDialogHide
HD.ProgressDialogShow("Searching for Devices, Please Wait.")
ScanningMode = True 'Scanning only
DiscoveryMode = False 'No discovery until connecting.
ScanCancelled = False
FoundDevices.Initialize
FoundDevices.Clear
servicelist.Add(BLEMasterService)
manager.Scan2(servicelist, True)
ScanTimer.Enabled = True
Do Until ScanCancelled = True
Wait For Device_Detected (ReturnCode As Int) 'Wait for any devices to be discovered.
If ReturnCode = 0 Then 'We continue to scan and add adapters.
HD.ProgressDialogShow("Searching for Devices. (" & FoundDevices.Size & " Found)") <-- This never executes or updates.
devices.Clear
For Each Key As String In FoundDevices.Keys
Dim DeviceInfo As Device
DeviceInfo.Initialize
DeviceInfo = FoundDevices.Get(Key)
devices.Add(DeviceInfo.Name & " " & GetBars(CalculateSignalLevel(DeviceInfo.RSSI, 5)))
#IF DEBUG
Log(DeviceInfo) <--- This works in debug, sometimes.
#End If
Next
Else 'The scan has been completed or cancelled.
ScanCancelled = True
ScanningMode = False
manager.StopScan
Exit
End If
Loop
HD.ProgressDialogHide <--- This never gets reached, or the code below it.
Log("Scan Complete")
sheet.Initialize("sheet", "Available Devices", "","", devices)
sheet.Show(Main.page1.RootPanel)
' Wait For sheet_Click (Result As String)
' Log(Result)
End Sub
Sub ScanTimer_Tick
ScanTimer.Enabled = False
CallSub2(Me, "Device_Detected", 1) 'Call our event to exit the loop on scan complete. (to prevent a "potential" memory leak from an unresolved Wait For. I feel better this way.)
End Sub
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
Dim UniqueID As String
If AdvertisingData.ContainsKey("kCBAdvDataManufacturerData") = True Then 'Make sure only our programmed devices can be discovered.
UniqueID = AdvertisingData.Get("kCBAdvDataManufacturerData")
UniqueID = Regex.Replace("[^0-9A-Fa-f]+", UniqueID, "").ToUpperCase
End If
If ScanningMode = True And UniqueID = "XXXXXXXXXX" Then 'We are scanning for valid devices. Add them one at a time into our list of available connections.
If FoundDevices.ContainsKey(Id) = False Then 'If the discovered device is not already in the list, then we add it and move on.
Dim DetectedDevice As Device
DetectedDevice.ID = Id
DetectedDevice.Name = Name
DetectedDevice.RSSI = RSSI
FoundDevices.Put(Id, DetectedDevice)
CallSub2(Me, "Device_Detected", 0) 'Call our Wait For event to add any new entries into the selection list. 0 means scan continue.
End If
Else If UniqueID = "XXXXXXXXXX" And SelectedDevice.ID = Id Then 'We have discovered our device
DiscoveryMode = True
manager.StopScan 'Stop our scan since we found the device we are looking for.
manager.Connect(Id) 'Connect to our device.
End If
End Sub