Android Tutorial Discovering iBeacon devices with BLE2 library

upload_2015-11-8_12-49-22.png



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:
B4X:
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/
 

Attachments

  • iBeacons.zip
    11.4 KB · Views: 1,960
Last edited:

bluedude

Well-Known Member
Licensed User
Longtime User
I guess this could also easily be used for detecting the Eddystone Beacon format by Google?
 

bluedude

Well-Known Member
Licensed User
Longtime User
Would be nice if someone could extend the sample to: AltBeacons and Eddystone beacons. That way we could support everything with one piece of code.
 

bluedude

Well-Known Member
Licensed User
Longtime User
I will check. What is the $ symbol for BTW? Is that a new language feature?
 

bluedude

Well-Known Member
Licensed User
Longtime User
This is the result:

Key: 1 = 06
Key: 3 = AAFE
Key: 22 = 0DD068714579333125
Key: 9 = 5448494E47532E494F
Key: 10 = 04
 

bluedude

Well-Known Member
Licensed User
Longtime User
Maybe, the most important part is the advertised URL and of course UUID etc.. Google's Beacons (Eddystone is their format) broadcasts an URL.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Kontak.io beacons can do both, iBeacon format and Eddystone. Great if you can implement it.
 

Artigala

Member
Licensed User
Longtime User
Dear Support,
How to know, with BLE2 if BLE is supported (as previous version with "manager.BleSupported" ?
Thank you
 

Licht2002

Member
Licensed User
Longtime User
Hi to all,

i test Erel`s Lib and Code.... with my BLE Beacon "MLE-15".... http://www.pboon.com/index.php?_m=mod_product&_a=view&p_id=223

B4X:
** Activity (main) Resume **
Found: MLE-15, FF:FF:F0:00:17:1D, RSSI = -67, (MyMap) {1=[B@3ac46f1b, 2=[B@295fb8b8, 9=[B@1e59e691, 0=[B@35ad89f6}
Discovering services.
Connected

Is it normal, that i have to push the Button on MLE-15 to get an Connect?

Is this only with this Beacon.....???

Thx

Tom
 

Licht2002

Member
Licensed User
Longtime User
Thx Erel,

which keyword to i have to search for...... is it possible to do such an autoconnect?

Thx
Tom
 
Top