Hello everyone, I am following this example to scan bluetooth devices. https://www.b4x.com/android/forum/threads/b4xpages-bluetooth-chat-example.119014/#content
The fact is that the code works fine, but when it shows the devices found it does so twice. I mean if it finds 4 devices it shows 8, twice each.
I don't know why it does it, if anyone knows I would like to correct it.
Thank you
The DeviceFound events come from the OS. My guess is that the underlying Bluetooth adapter sends duplicate events. This can be solved with a Map or B4XSet:
B4X:
Private Sub Admin_DeviceFound (Name As String, MacAddress As String)
Log(Name & ":" & MacAddress)
Dim nm As NameAndMac = CreateNameAndMac(Name, MacAddress)
If SetOfMacs.Contains(MacAddress) Then Return
SetOfMacs.Add(MacAddress)
CLV.AddTextItem($"${nm.Name}: ${nm.Mac}"$, nm)
End Sub
Thanks Erel, I solved it with this code that you put in another post.
B4X:
Private Sub Admin_DeviceFound (Name As String, MacAddress As String)
Log(Name & ":" & MacAddress)
Dim nm As NameAndMac = CreateNameAndMac(Name, MacAddress)
CLV.AddTextItem(nm.Name & CRLF & nm.Mac, nm)
Dim m As Map
m.Initialize
Dim i As Int = 0
Do While i < CLV.GetSize
Dim item As String = CLV.GetValue(i)
If m.ContainsKey(item) Then
CLV.RemoveAt(i)
i = i - 1
Else
m.Put(item, "")
End If
i = i + 1
Loop
End Sub