Presuming your ESP32 BLE devices sharing the same advertising name you could add a filter on found devices, like:
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
'Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData)
Dim nm As NameAndMac
nm.Initialize
nm.Name = Name
nm.Mac = Id
If nm.Name = "yourDeviceAdvertisingName" Then ' filter heard one by name
heardDeviceList.Add(nm) ' add it to a list
combineHeardDeviceList.Add(nm.Name & " - "&nm.Mac) ' and add also the coresponding MAC heard,
' so you can differentiate later
End If
End Sub
Now, what I do is using a timer for the scan timeout, so I can choose which BLE device I want to connect to from the list above
' declare a timer
Dim scanTimeout As Timer
' initialize the timer
Public Sub Initialize
'...
scanTimeout.Initialize("scanTimeout", 3000) ' arbitrary 3 seconds period
'...
End Sub
' enable the timer on a Scan Start
Public Sub StartScan
If manager.State <> manager.STATE_POWERED_ON Then
Log("Not powered on.")
Else
Log("Start SCanning")
scanTimeout.Enabled = True ' start this timer
manager.Scan2( Null, False) ' don't allow duplicates
End If
End Sub
And when the Timer triggers this sub gets called, which in turn allows you to choose and connect to the respective BLE device
private Sub scanTimeout_Tick
manager.StopScan ' say we heard'em all, can stop scanning for now
StateChanged
scanTimeout.Enabled = False ' and stop this timer
If connected = False Then
InputListAsync(combineHeardDeviceList, "Choose device to connect", -1, True) ' show list and choose device
Wait For InputList_Result (Index As Int)
If Index <> DialogResponse.CANCEL Then
Dim device As NameAndMac
device.Initialize
device = heardDeviceList.Get(Index)
ConnectedName = device.Name ' connected BLE device Name
ConnectedID = device.Mac ' connected BLE MAC address
Label_Name_Val.Text = ConnectedName ' here I show values on user UI
Label_MAC_Val.Text = ConnectedID '
'LogColor("connecting to: "&ConnectedName&" - "&ConnectedID, Colors.Green)
manager.Connect2(device.Mac, False) ' here you start connecting to chosen device
' disabling auto connect can make the connection quicker
ProgressDialogShow2($"Trying to connect to: ${device.Name} (${device.Mac})"$, False) ' user UI related
Sleep(3000) ' some drama
ProgressDialogHide
End If
'
heardDeviceList.clear
combineHeardDeviceList.Clear
End If
End Sub
As for the Service and Characteristics,
to get the DataAvailable your ESP32 could use Notify or you could read the needed Characteristic with something like:
manager.ReadData2(Service, Characteristic)
Here is an example on checking against the standard Info Service, UUID = "0000180a-0000-1000-8000-00805f9b34fb" ,
and for your example defined Service and Characteristic UUID
' Your Service and Characteristic
Public SERVICE_UUID As String = "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
Public CHARACTERISTIC_UUID as String = "beb5483e-36e1-4688-b7f5-ea07361b26a8"
' DEVICE INFORMATION SERVICE
Public INFO_SERVICE_UUID As String = "0000180a-0000-1000-8000-00805f9b34fb"
' INFORMATION SERVICE characteristics
Public MODEL_NUMBER As String = "00002a24-0000-1000-8000-00805f9b34fb" ' Model Number String
Public SERIAL_NUMBER As String = "00002a25-0000-1000-8000-00805f9b34fb" ' Serial Number String
Public FIRMWARE_REVISION As String = "00002a26-0000-1000-8000-00805f9b34fb" ' Firmware Revision String
Public HARDWARE_REVISION As String = "00002a27-0000-1000-8000-00805f9b34fb" ' Software Revision String
Public SOFTWARE_REVISION As String = "00002a28-0000-1000-8000-00805f9b34fb" ' Software Revision String
Public MANUFACTURER As String = "00002a29-0000-1000-8000-00805f9b34fb" ' Manufacturer Name String
Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
Dim bc As ByteConverter
' Log("+++++++++++++++++++++++++++++++++++++++++")
' Log("sid = "&ServiceId)
' LogColor("characteristics size = "&Characteristics.Size, Colors.yellow)
If ServiceId = INFO_SERVICE_UUID Then ' check if it is a InfoService UUID
For Each id As String In Characteristics.Keys
Dim dataContent() As Byte = Characteristics.Get(id)
Select id
Case MODEL_NUMBER
conModelNumber = BytesToString(dataContent, 0, dataContent.Length, "UTF8")
Log("Model: " & conModelNumber)
Case SERIAL_NUMBER
conSerialNumber = BytesToString(dataContent, 0, dataContent.Length, "UTF8")
Log("Serial no.: " & conSerialNumber)
Case FIRMWARE_REVISION
conFirmwareRevision = BytesToString(dataContent, 0, dataContent.Length, "UTF8")
Log("FW rev: " & conFirmwareRevision)
Case HARDWARE_REVISION
conHardwareRevision = BytesToString(dataContent, 0, dataContent.Length, "UTF8")
Log("HW rev: " & conHardwareRevision)
Case SOFTWARE_REVISION
conSoftwareRevision = BytesToString(dataContent, 0, dataContent.Length, "UTF8")
Log("SW rev: " & conSoftwareRevision)
Case MANUFACTURER
conManufacturerName = BytesToString(dataContent, 0, dataContent.Length, "UTF8")
Log("Manufacturer: " & conManufacturerName)
End Select
' update some labels on UI
Label_Model_Val.Text = conModelNumber
Label_SerialNo_Val.Text = conSerialNumber
Label_Firmware_Val.Text = conFirmwareRevision
Label_Hardware_Val.Text = conHardwareRevision
Label_Software_Val.Text = conSoftwareRevision
Label_Manufacturer_Val.Text = conManufacturerName
Next
End If
If ServiceId = SERVICE_UUID Then ' check if it your Service UUID
If Characteristics.ContainsKey(CHARACTERISTIC_UUID) Then ' and if Characteristic is the one you're after
If Characteristics.Getvalueat(0)<>Null Then
'LogColor("Got my Service and Characteristic UUID I was after ...", Colors.blue)
Dim b() As Byte = Characteristics.Getvalueat(0) ' here you can get the Characteristic data and an array
' for further processing ...
'...
End If
End If
End If
End If
End Sub
Cheers