could anyone provide please an example on how to list all detected (scanned) devices using BLE
to an AsyncInputList, so that the user can select the device that he/she wishes to connect to.
Sub Scan
FoundDevices.Initialize 'process global Map variable
manager.Scan2(Null, False)
Sleep(10000)
manager.StopScan
CallSubDelayed2(Main, "AfterScan", FoundDevices)
End Sub
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
Log($"Device found: ${Name}"$)
FoundDevices.Put(Name, Id)
End Sub
Code in Main:
B4X:
Sub AfterScan(Devices As Map)
Dim items As List
items.Initialize
For Each name As String In Devices.Keys
items.Add(name)
Next
If items.Size > 0 Then
InputListAsync(items, "Select Device", 0, False)
Wait For InputList_Result (Index As Int)
If Index <> DialogResponse.CANCEL Then
Dim id As String = Devices.Get(items.Get(Index))
Log("Connect to: " & id)
End If
End If
End Sub