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,476
Last edited:

luke2012

Well-Known Member
Licensed User
Within my app I wish to track only a specific beacon selected by the user (for example when the user start the app for the first time).
Is it possible using this library?

thanks in advance for your reply :)
 

luke2012

Well-Known Member
Licensed User
B4X:
If beacon1.BeaconType = 0 Then
    key = 22
    If AdvertisingData.ContainsKey(key) Then
        For Each b() As Byte In Starter.BLE2Mgr.GetRecordsFromAdvertisingData(AdvertisingData, key)
            Log(bc.HexFromBytes(b))
            If CompareBytes(b, Array As Byte(0xAA, 0xFE), 0) Then
                'Eddystone
                tx = ParseEddystone(beacon1, b, 0)
            End If
       Next
    End If
End If

Hi all,
what is the value "0A181204CC1E0000" Log(bc.HexFromBytes(b)) printed to the log ?
 

luke2012

Well-Known Member
Licensed User
I'm testing it with two iBeacon. I'm logging the uniqueid when the _DeviceFound event will be raised.
But I can see always the same uniqueid.

I suppose that each iBeacon have a different uniqueid value right?
My final target is to set the uniqueid within the code in order to process only a specific iBeacon device also if the BLE Manager found other Beacons or devices.

Within the B4A log I can see only this uniqueid: FDA50693A4E24FB1AFCFC6EB0764782500010001
 
Last edited:

luke2012

Well-Known Member
Licensed User
Hi all,
which is the best solution (using this class / sample code) to track only a specific iBeacon using the uniqueid?

1) Set an uniqueid passed by the the user (input dialog) the at the first app installation (for ex. user can read a printed uniqueid on his iBeacon) ?
2) Show a dialog that display the discovered iBeacon and let the user choose the uniqueid related to his iBeacon ?

Thanks in advance for your reply :)
 

luke2012

Well-Known Member
Licensed User
How many beacons do you expect there to be near the user? I think that it is better to show all and let the user filter if he likes.

I think few iBeacons (less than 5).
I'm planning an app that implement a "pairing" with only one specific iBeacon that is set by the user at the first installation.
 
Last edited:

luke2012

Well-Known Member
Licensed User
Hi.Which is the BLE working range (10 meters) ?
How to say to the user that the beacon is out of range considering that the device isn't connected to the device?
 
Last edited:
Top