A andyp Member Licensed User Longtime User May 21, 2019 #1 Hi Currently my BT app is hard coded for the expected BT name (HC-05). I wish to be able to select from a range of paired BT devices (with the same or different names - obviously each with a unique MAC address) Using the example code form here https://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/#content I get a selection list - but devices are listed multiple times. On the last page of the thread, there is a suggestion to use a 'map' to prevent duplicates. I cant get this to work - I wont post code as I am just guessing and cant figure out how to implement this...... Would someone be kind enough to update the original example, using a map to prevent duplicates in the BT device list? Thank you! Andrew
Hi Currently my BT app is hard coded for the expected BT name (HC-05). I wish to be able to select from a range of paired BT devices (with the same or different names - obviously each with a unique MAC address) Using the example code form here https://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/#content I get a selection list - but devices are listed multiple times. On the last page of the thread, there is a suggestion to use a 'map' to prevent duplicates. I cant get this to work - I wont post code as I am just guessing and cant figure out how to implement this...... Would someone be kind enough to update the original example, using a map to prevent duplicates in the BT device list? Thank you! Andrew
Erel B4X founder Staff member Licensed User Longtime User May 21, 2019 #2 Can you post the code from the example that adds the devices to a list? Upvote 0
A andyp Member Licensed User Longtime User May 21, 2019 #3 This is the bit of code from 'Bluetoothmanager' (class) where devices are added to a list: Code: Private Sub Admin_DeviceFound (Name As String, MacAddress As String) Log(Name & ":" & MacAddress) Dim nm As NameAndMac nm.Name = Name nm.Mac = MacAddress foundDevices.Add(nm) End Sub Unfortunately the list has multiple identical entries..... Thank you Andrew Upvote 0
This is the bit of code from 'Bluetoothmanager' (class) where devices are added to a list: Code: Private Sub Admin_DeviceFound (Name As String, MacAddress As String) Log(Name & ":" & MacAddress) Dim nm As NameAndMac nm.Name = Name nm.Mac = MacAddress foundDevices.Add(nm) End Sub Unfortunately the list has multiple identical entries..... Thank you Andrew
Erel B4X founder Staff member Licensed User Longtime User May 22, 2019 #4 Change foundDevices to be a Map. Code: Private Sub Admin_DeviceFound (Name As String, MacAddress As String) Log(Name & ":" & MacAddress) Dim nm As NameAndMac nm.Name = Name nm.Mac = MacAddress foundDevices.Put(nm.Name, nm) End Sub You can convert it back to a list when needed: Code: Dim devices As List devices.Initialize For Each nm As NameAndMac in foundDevices.Values devices.Add(nm) Next Upvote 0
Change foundDevices to be a Map. Code: Private Sub Admin_DeviceFound (Name As String, MacAddress As String) Log(Name & ":" & MacAddress) Dim nm As NameAndMac nm.Name = Name nm.Mac = MacAddress foundDevices.Put(nm.Name, nm) End Sub You can convert it back to a list when needed: Code: Dim devices As List devices.Initialize For Each nm As NameAndMac in foundDevices.Values devices.Add(nm) Next