This example uses the BLE2 library to continuously scan for BLE devices, looking for iBeacons.
It never connects to any device. It parses the advertising data and calculates the distance based on the RSSI level (it is pretty inaccurate).
The important code is in the Starter service module.
It starts scanning with the AllowDuplicates parameter set to true.
Whenever a device is found, the data is parsed and it raises a StateChanged event:
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double) Dim key As Int = -1 'type is important. Must be Int. If AdvertisingData.ContainsKey(key) 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 beacon As Beacon beacon.Initialize Dim raf As RandomAccessFile raf.Initialize3(b, False) Dim hex As String = bc.HexFromBytes(b) beacon.uuid = hex.SubString2(8, 40) 'bytes 4 - 19 beacon.uniqueid = hex.SubString2(8, 48) 'this also includes the major and minor parts beacon.major = raf.ReadShort(20) beacon.minor = raf.ReadShort(22) Dim tx As Byte = raf.ReadSignedByte(24) beacon.distance = CalculateDistance(tx, RSSI) beacon.time = DateTime.Now beacons.Put(beacon.uniqueid, beacon) CallSub(Main, "StateChanged") End If End If End Sub
In the Activity code, we go over the beacons and create or update CLV items.
Beacons that were not updated in the last 30 seconds are removed.
The example depends on ByteConverter library.
It is recommended to use the BeaconParser class instead of the code above: https://www.b4x.com/android/forum/t...iscover-ibeacons-and-eddystone-beacons.61127/