Android Question Detecting BT Beacons using BLE2

james_sgp

Active Member
Licensed User
Longtime User
I am experimenting with BT iBeacons, I have a few to test with. But I`m getting a problem that the code isn`t recognizing the "-1" in the AdvertisingData, here is my code:

B4X:
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log("Device: " & Name & " | Advertising: " & AdvertisingData)
    If Name = "Holy-IOT" Then
        Log("Found: " & Name & " | Advertising: " & AdvertisingData)
        
        Dim bc As ByteConverter
        Dim key As Int = -1 'type is important. Must be Int.
        If AdvertisingData.ContainsKey(key) = True Then
            Dim b() As Byte = AdvertisingData.Get(key)
            If b.Length > 4 And b(0) = 0x4c And b(1) = 0 And b(2) = 0x02 And b(3) = 0x15 Then
                Dim beacon2 As Beacon
                beacon2.Initialize
                Dim raf As RandomAccessFile
                raf.Initialize3(b, False)
                Dim hex As String = bc.HexFromBytes(b)
                beacon2.uuid = hex.SubString2(8, 40) 'bytes 4 - 19
                beacon2.uniqueid = hex.SubString2(8, 48) 'this also includes the major and minor parts
                beacon2.major = raf.ReadShort(20)
                beacon2.minor = raf.ReadShort(22)
                Dim tx As Byte = raf.ReadSignedByte(24)
                beacon2.distance = CalculateDistance(tx, RSSI)
                beacon2.time = DateTime.Now
            End If
        End If

    '    Dim tmp As String = Name & " | ID = " & beacon2.uniqueid '& " | Distance = " & beacon2.distance
    '    clv.AddTextItem(tmp,0)
    End If

End Sub

Here is the log, that I`m getting; there is definitely "-1" in the map; the code detects other keys if i change the number. My code filters, for the beacons called "Holy-IOT", but I`m a little stumped with why I can`t access the -1 map...

B4X:
Device: HUAWEI Band 6-849 | Advertising: (MyMap) {1=[B@fb2aa63, -1=[B@a8ee660, 22=[B@7498719, 10=[B@7ce53de, 9=[B@c1b89bf, 0=[B@46c088c}
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Device:  | Advertising: (MyMap) {3=[B@8aaeab1, 22=[B@a2e0b96, -1=[B@926d317, 0=[B@4a31304}
Device:  | Advertising: (MyMap) {-1=[B@4b74b22, 0=[B@93b89b3}
[B]Device: Holy-IOT | Advertising: (MyMap) {1=[B@7633ee9, -1=[B@3a2f76e, 9=[B@db0e0f, 22=[B@a27be9c, 0=[B@7a7e0a5}
Found: Holy-IOT | Advertising: (MyMap) {1=[B@7633ee9, -1=[B@3a2f76e, 9=[B@db0e0f, 22=[B@a27be9c, 0=[B@7a7e0a5}[/B]
Device: [TV] Samsung AU7000 75 TV | Advertising: (MyMap) {1=[B@7447c2b, -1=[B@a402888, 8=[B@adae221, 0=[B@69e8646}
Device:  | Advertising: (MyMap) {-1=[B@fe27f5d, 0=[B@52cc0d2}
Device:  | Advertising: (MyMap) {-1=[B@3ccb459, 0=[B@2f2181e}
Device:  | Advertising: (MyMap) {1=[B@aa606cc, 3=[B@c8b3d15, 22=[B@723982a, 0=[B@d00c61b}
[B]Device: Holy-IOT | Advertising: (MyMap) {1=[B@45ba8f7, -1=[B@e8c6364, 9=[B@b66f9cd, 22=[B@c830282, 0=[B@117dd93}
Found: Holy-IOT | Advertising: (MyMap) {1=[B@45ba8f7, -1=[B@e8c6364, 9=[B@b66f9cd, 22=[B@c830282, 0=[B@117dd93}[/B]
Device: IGM1-C2A5P1_fa5f69 | Advertising: (MyMap) {1=[B@4531ac1, 10=[B@bcf6266, 2=[B@82c29a7, -1=[B@da8ce54, 9=[B@8b5a1fd, 18=[B@35f3ef2, 0=[B@5e7b943}
Device: LG HS8(CB:6F) | Advertising: (MyMap) {1=[B@cfecd9e, -1=[B@157e07f, 9=[B@a0b784c, 0=[B@c519095}
Device:  | Advertising: (MyMap) {1=[B@db9ee86, 3=[B@7b9347, 22=[B@b678774, -1=[B@e12349d, 0=[B@8366d12}
Device: HUAWEI Band 6-849 | Advertising: (MyMap) {1=[B@e1995d8, -1=[B@99ccd31, 22=[B@32e7c16, 10=[B@39db997, 9=[B@a7e3784, 0=[B@c00b6d}

James
 

emexes

Expert
Licensed User

I'm wondering if you might be on to something with "type is important". What happens if key (aka advertising data type?) is Byte rather than Int? I'm pretty certain that the advertising data types are just single bytes in the advertising packet.
 
Last edited:
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Thanks all, Erels suggestion worked. But here is something interesting for those playing with beacons.
Using the Holy-IOT app i connected to the beacons and found they were set in 'Beacon' mode, i changed it to 'iBeacon' mode and Erels code works as posted. So guessing somehow the AdvertisingData tag is different in Beacon & iBeacon mode.

Thanks, James
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…