iOS Question iBLE devicefound mac adress

MList

Member
Licensed User
Hello, i am trying to find the mac adress in ible Devicefound
In B4a id contains the mac adress. In B4i id contains something different, but not Mac Adress
We have several devices (same type, same Name, same services) and have to choose which one to connect, thats why i show
the last 4 characters of Mac Adress.
How can i do this with B4I ?


B4X:
Public Sub Manager_DeviceFound (Name As String, id As String, AdvertisingData As Map, RSSI As Double)
     Dim na(20) As String
    Dim i,k As Int
            If Name.StartsWith("TO") Or Name.StartsWith("MEGA") Or Name.StartsWith("MP")  Or Name.StartsWith("FE") Or Name.StartsWith("Fe") Or Name.StartsWith(".")  Or Name.StartsWith("Ho") Or Name.StartsWith("HO") Then
            PSelect.clvSelect.DefaultTextBackgroundColor = Var.limadarkblue
            #if b4i
    '            Log(AdvertisingData.Values)
                PSelect.clvSelect.add(PSelect.CreateListItem(Name  , "D"),id & "/" & Name)
            #Else
                PSelect.clvSelect.add(PSelect.CreateListItem(Name & " (" &  id.SubString(12) & ")" , "D"),id & "/" & Name)
            #end if
        Else   
            PSelect.clvSelect.DefaultTextBackgroundColor = Var.limablue
            PSelect.clvSelect.add(PSelect.CreateListItem(Name ,"T"),id & "/" & Name)
        End If
        PSelect.clvSelect.Refresh
end sub

Thanks for help
Marion
 
Solution
I wouldn't rely on the device's mac address in order to uniquely identify a ble device with iOS
Instead, a good practice is to customize one of the advertisingData fields, by coding info into it, such as the device type and for instance serial number and/or the mac address last digits.

Filippo

Expert
Licensed User
Longtime User
Hello, i am trying to find the mac adress in ible Devicefound
In B4a id contains the mac adress. In B4i id contains something different, but not Mac Adress
We have several devices (same type, same Name, same services) and have to choose which one to connect, thats why i show
the last 4 characters of Mac Adress.
How can i do this with B4I ?


B4X:
Public Sub Manager_DeviceFound (Name As String, id As String, AdvertisingData As Map, RSSI As Double)
     Dim na(20) As String
    Dim i,k As Int
            If Name.StartsWith("TO") Or Name.StartsWith("MEGA") Or Name.StartsWith("MP")  Or Name.StartsWith("FE") Or Name.StartsWith("Fe") Or Name.StartsWith(".")  Or Name.StartsWith("Ho") Or Name.StartsWith("HO") Then
            PSelect.clvSelect.DefaultTextBackgroundColor = Var.limadarkblue
            #if b4i
    '            Log(AdvertisingData.Values)
                PSelect.clvSelect.add(PSelect.CreateListItem(Name  , "D"),id & "/" & Name)
            #Else
                PSelect.clvSelect.add(PSelect.CreateListItem(Name & " (" &  id.SubString(12) & ")" , "D"),id & "/" & Name)
            #end if
        Else  
            PSelect.clvSelect.DefaultTextBackgroundColor = Var.limablue
            PSelect.clvSelect.add(PSelect.CreateListItem(Name ,"T"),id & "/" & Name)
        End If
        PSelect.clvSelect.Refresh
end sub

Thanks for help
Marion
Here is a little help:
B4X:
Sub Class_Globals
    Type NameAndMac (Name As String, Mac As String)
    Public btManager As BleManager
    Private FoundDevices As List
End Sub

Sub Scan
    FoundDevices.Initialize
    btManager.Scan2(Null, False)
    Sleep(5000)
    btManager.StopScan
    CallSubDelayed2(Me, "AfterScan", FoundDevices)
End Sub

Private Sub btManager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    'Log($"Device found: ${Name}"$)
    Dim NameAndMac1 As NameAndMac
    NameAndMac1.Initialize
    NameAndMac1.Mac = Id
    NameAndMac1.Name = AdvertisingData.Get("kCBAdvDataLocalName").As(String).Trim
    FoundDevices.Add(NameAndMac1)
End Sub

Private Sub AfterScan(Devices As List)
    For i = 0 To Devices.Size - 1
        Dim NameAndMac1 As NameAndMac = Devices.Get(i)
        If NameAndMac1.Name.StartsWith("TO") Or NameAndMac1.Name.StartsWith("MEGA") Or NameAndMac1.Name.StartsWith("MP")  Or NameAndMac1.Name.StartsWith("FE") Or NameAndMac1.Name.StartsWith("Fe") Or NameAndMac1.Name.StartsWith(".")  Or NameAndMac1.Name.StartsWith("Ho") Or NameAndMac1.Name.StartsWith("HO") Then
            btManager.Connect(NameAndMac1.Mac)
       End If
    Next
End Sub
 
Upvote 0

MList

Member
Licensed User
Here is a little help:
B4X:
Sub Class_Globals
    Type NameAndMac (Name As String, Mac As String)
    Public btManager As BleManager
    Private FoundDevices As List
End Sub

Sub Scan
    FoundDevices.Initialize
    btManager.Scan2(Null, False)
    Sleep(5000)
    btManager.StopScan
    CallSubDelayed2(Me, "AfterScan", FoundDevices)
End Sub

Private Sub btManager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    'Log($"Device found: ${Name}"$)
    Dim NameAndMac1 As NameAndMac
    NameAndMac1.Initialize
    NameAndMac1.Mac = Id
    NameAndMac1.Name = AdvertisingData.Get("kCBAdvDataLocalName").As(String).Trim
    FoundDevices.Add(NameAndMac1)
End Sub

Private Sub AfterScan(Devices As List)
    For i = 0 To Devices.Size - 1
        Dim NameAndMac1 As NameAndMac = Devices.Get(i)
        If NameAndMac1.Name.StartsWith("TO") Or NameAndMac1.Name.StartsWith("MEGA") Or NameAndMac1.Name.StartsWith("MP")  Or NameAndMac1.Name.StartsWith("FE") Or NameAndMac1.Name.StartsWith("Fe") Or NameAndMac1.Name.StartsWith(".")  Or NameAndMac1.Name.StartsWith("Ho") Or NameAndMac1.Name.StartsWith("HO") Then
            btManager.Connect(NameAndMac1.Mac)
       End If
    Next
End Sub
Thanks for your help but i don't understand : My device has MAC 04:0D:84:77:6E:15
when using B4a id = 04:0D:84:77:6E:15,
when using B4i then id = 69CAC4C3-B61D-7E20-1F95-CD8C321A8C0A

I need to see 04:0D:84:77:6E:15 when scanning the device, the number in B4i doesnt say anything to the user of app.
Even after stop scan its still the wrong number in NameAndMac1.Mac = 69CAC4C3-B61D-7E20-1F95-CD8C321A8C0A

Did i misunderstood something ?

 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
I wouldn't rely on the device's mac address in order to uniquely identify a ble device with iOS
Instead, a good practice is to customize one of the advertisingData fields, by coding info into it, such as the device type and for instance serial number and/or the mac address last digits.
 
Upvote 0
Solution

MList

Member
Licensed User
I wouldn't rely on the device's mac address in order to uniquely identify a ble device with iOS
Instead, a good practice is to customize one of the advertisingData fields, by coding info into it, such as the device type and for instance serial number and/or the mac address last digits.
Thats a very good idea, thanks
 
Upvote 0
Top