Android Question Android Bluetooth - Device Selection

Discussion in 'Android Questions' started by andyp, May 21, 2019.

  1. andyp

    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the code from the example that adds the devices to a list?
     
  3. andyp

    andyp Member Licensed User

    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    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
     
    DonManfred, Peter Simpson and andyp like this.
  5. andyp

    andyp Member Licensed User

    Thank you, sorted!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice