Android Tutorial Discovering iBeacon devices with BLE2 library

Erel

Administrator
Staff member
Licensed User
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: http://www.b4x.com/android/forum/threads/iot-beaconparser-discover-ibeacons-and-eddystone-beacons.61127/
 

Attachments

Last edited:

bluedude

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

bluedude

Well-Known Member
Licensed 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.
 

Erel

Administrator
Staff member
Licensed User
I don't have here such a beacon however it shouldn't be complicated to use similar code to interact with any type of beacon. You just need to change the parsing code.
 

Erel

Administrator
Staff member
Licensed User
What is the output of the following code:
B4X:
Dim bc As ByteConverter
For Each Key As Int In AdvertisingData.Keys
 Dim b() As Byte = AdvertisingData.Get(Key)
 Log($"Key: ${Key} = ${bc.HexFromBytes(b))}"$)
Next
 

bluedude

Well-Known Member
Licensed User
This is the result:

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

Erel

Administrator
Staff member
Licensed User
I will check it. It seems that the structure is a bit more complex than iBeacon.
 

bluedude

Well-Known Member
Licensed 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.
 

Erel

Administrator
Staff member
Licensed User
I've ordered such a beacon. Once it will arrive (several weeks probably) I will implement the parser.
 

bluedude

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

Artigala

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

Erel

Administrator
Staff member
Licensed User
How to know, with BLE2 if BLE is supported (as previous version with "manager.BleSupported" ?
It is possible. However you are posting in the wrong place. Please start a new thread for this question.
 

Licht2002

Member
Licensed 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
 

Erel

Administrator
Staff member
Licensed User
This example doesn't connect. It only parses the advertising data.
 

Licht2002

Member
Licensed User
Thx Erel,

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

Thx
Tom
 
Top