iOS Question [B4X] BLE 2 - Concept of "ID" different between iOS and Android...

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi everyone,
I'm developing an app that uses the BLE, i'm using the last suggested way: this.

However... i need to connect to a specific device knowing the MAC Address...
I made the App for Android and everything works because when the sub "_DeviceFound" fires it returns something like:
potty_android.jpg

where the "id" variable is indeed the MAC Address, so i can detect it and use to connect.

Now... same thing done in iOS, but the result of "_DeviceFound" is:
potty_ios.jpg


What is that number after the device name?? it is not the MAC address like in Android... and I need it to detect what device to connect to

How can I get the MAC address in iOS too? what type of ID is the one given by iOS?

Thanks in advance
 
Solution
Thanks to @Star-Dust for another topic I managed to solve this issue like so:

B4X:
Private Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData) 'ignore
    
    Dim MAC As String = ""
    #if B4A
        MAC = Id
    #else if B4i
        If (AdvertisingData.ContainsKey("kCBAdvDataManufacturerData")) And (Name == DeviceExpectedName) Then
            Dim s As String = AdvertisingData.Get("kCBAdvDataManufacturerData")
            Dim data_raw() As String = Regex.Split(", ", s.Replace("}","").Replace("{", ""))
            Dim mac_string As String = Regex.Split(" = ", data_raw(1))(1).Trim...

Mike1970

Well-Known Member
Licensed User
Longtime User
Bad news...
I found the reason here

so.. it seems that the only way is: You must have the possibility to put your hands on the device firmware.
Fortunately this is my case, given the fact i'm using an ESP32 (Arduino framework).

So in this case what you can do is to add some "AdvertisingData" that you can pick also in the "_DeviceFound" sub.

But at the moment I see the data in the logs but if i try to do:
B4X:
AdvertisingData.Get("kCBAdvDataManufacturerData")
it returns something strange... of type "_NSInlineData"...
if i try to cast it to Map it works but as soon as i try to use some Map methods it crashes saying that the method does not exists... so... i see the data but i don't know how to get it...


On ESP32:
C++:
// ... All BLE setup
BLEAdvertisementData pAdvertisementData;
pAdvertisementData.setManufacturerData(MAC);
// Last BLE things like startAdvertising etc ...
P.S. MAC is a variabile where I stored the BLE MAC address..
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Thanks to @Star-Dust for another topic I managed to solve this issue like so:

B4X:
Private Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData) 'ignore
    
    Dim MAC As String = ""
    #if B4A
        MAC = Id
    #else if B4i
        If (AdvertisingData.ContainsKey("kCBAdvDataManufacturerData")) And (Name == DeviceExpectedName) Then
            Dim s As String = AdvertisingData.Get("kCBAdvDataManufacturerData")
            Dim data_raw() As String = Regex.Split(", ", s.Replace("}","").Replace("{", ""))
            Dim mac_string As String = Regex.Split(" = ", data_raw(1))(1).Trim
            
            Dim BC As ByteConverter
            Dim data() As Byte = BC.HexToBytes(mac_string.SubString(2))

            For Each b As Byte In data
                MAC=MAC & Chr(b)
            Next
        End If
    #End If
    

    If MAC == DeviceToConnect Then
        ConnectedName = Name
        'Log("BLE connecting")
        #if B4A
            manager.Connect2(Id, False) 'disabling auto connect can make the connection quicker
        #else if B4I
            manager.Connect(Id)
        #end if
        
        manager.StopScan
    End If
End Sub
 
Upvote 0
Solution
Top