Android Question Android Bluetooth - Device Selection

andyp

Member
Licensed User
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
 

andyp

Member
Licensed User
This is the bit of code from 'Bluetoothmanager' (class) where devices are added to a list:

B4X:
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

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change foundDevices to be a Map.
B4X:
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:
B4X:
Dim devices As List
devices.Initialize
For Each nm As NameAndMac in foundDevices.Values
 devices.Add(nm)
Next
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…