Android Tutorial Discovering iBeacon devices with BLE2 library

Discussion in 'Tutorials & Examples' started by Erel, Nov 8, 2015.

  1. Erel

    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:
    Code:
    Sub Manager_DeviceFound (Name As StringId 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(
    840'bytes 4 - 19
           beacon.uniqueid = hex.SubString2(848'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/th...iscover-ibeacons-and-eddystone-beacons.61127/
     

    Attached Files:

    Last edited: Dec 7, 2015
    jjcc, DonManfred and maXim like this.
  2. Caronte

    Caronte Member Licensed User

    How inaccurate? Meters? Centimeters? Random distance?
    Thanks!
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    luke2012 likes this.
  4. bluedude

    bluedude Well-Known Member Licensed User

    I guess this could also easily be used for detecting the Eddystone Beacon format by Google?
     
  5. bluedude

    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.
     
  6. Erel

    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.
     
  7. bluedude

    bluedude Well-Known Member Licensed User

  8. Erel

    Erel Administrator Staff Member Licensed User

    What is the output of the following code:
    Code:
    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
     
  9. bluedude

    bluedude Well-Known Member Licensed User

    I will check. What is the $ symbol for BTW? Is that a new language feature?
     
  10. bluedude

    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
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    I will check it. It seems that the structure is a bit more complex than iBeacon.
     
  12. bluedude

    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.
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    I've ordered such a beacon. Once it will arrive (several weeks probably) I will implement the parser.
     
    walterf25 likes this.
  14. bluedude

    bluedude Well-Known Member Licensed User

    Kontak.io beacons can do both, iBeacon format and Eddystone. Great if you can implement it.
     
  15. Artigala

    Artigala Member Licensed User

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

    DonManfred Expert Licensed User

    If ble2 does not have such a method then you could use the old manager in addition... Check if supported with the manager and do the work with ble2 then
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    It is possible. However you are posting in the wrong place. Please start a new thread for this question.
     
  18. Licht2002

    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

    Code:
    ** 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
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    This example doesn't connect. It only parses the advertising data.
     
  20. Licht2002

    Licht2002 Member Licensed User

    Thx Erel,

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

    Thx
    Tom
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice