B4A Class [IoT] BeaconParser - Discover iBeacons and Eddystone beacons

The BeaconParser class parses the advertising data that is sent by BLE beacons.
It supports iBeacons, Eddystone UID and Eddystone URL formats.

Note that this class is also compatible with B4i. However it will not detect iBeacons due to the way those beacons are handled in iOS.

You do not need to connect to the beacon. All the important information is delivered in the advertising data.
Call BleManager2.Scan2(Null, True) to start scanning. The second parameter (AllowDuplicates) means that it will "find" the same devices again and again.

B4A example:

upload_2015-12-7_13-4-49.png


B4i example:

upload_2015-12-7_13-5-13.png


The distance is not accurate enough to be useful.

The main method of BeaconParser is Parse. You should call it from the DeviceFound sub:
B4X:
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
   Dim beacon1 As Beacon = Parser.Parse(AdvertisingData, RSSI)
   If beacon1 <> Null Then
    'a beacon was discovered.
    'find the beacon type based on the BeaconType field.
  Select beacon1.BeaconType
     Case Parser.TypeiBeacon
       Dim ib As iBeacon = beacon1.SpecificData
       Log($"iBeacon: ${ib.uuid} (${ib.major}/${ib.minor})"$)
     Case Parser.TypeEddystoneUID
       Dim euid As EddystoneUID = beacon1.SpecificData
       lLog($"Eddystone UID: ${euid.Namespace}:${euid.Instance}"$)
     Case Parser.TypeEddystoneURL
       Dim eurl As EddystoneURL = beacon1.SpecificData
      Log($"Eddystone URL: ${eurl.url}"$)
   End Select
   End If
End Sub

New version of BeaconParser was uploaded. It requires BLE v1.20+: https://www.b4x.com/android/forum/threads/ble-2-bluetooth-low-energy.59937/#post-377705

It fixes an issue when there are multiple records with the same type.
 

Attachments

  • BeaconParser_B4i.zip
    7.2 KB · Views: 339
  • BeaconParser_B4A.zip
    7.3 KB · Views: 448
Last edited:

bluedude

Well-Known Member
Licensed User
Hi, I tested a few other Eddystone scanners and they all seem to recognize my beacons and the Eddystone URL. Maybe the BLE2.jar skips a few packages?
 

bluedude

Well-Known Member
Licensed User
Done that but does not work actually. Does not recognize a UID or URL and ParseEddystone does not work. Pretty much stuck right now.

Can you do something with the output I logged in previous messages?
 

bluedude

Well-Known Member
Licensed User
Hi, I have done all these steps and also posted the key 0 log in here. This does not solve my problem because none of the data matches or can be parsed it seems.
 

bluedude

Well-Known Member
Licensed User
Here is the output:

new device
1= 06
3= AAFE
22= 0DD0506F3057333164
9= 4B6F6E74616B74
10= 04
0= 0201060303AAFE0F16AAFE10EE007468696E67732E696F08094B6F6E74616B74020A040A160DD0506F305733316400000000000000000000000000000000
new device
1= 06
-1= 4C000215F7826DA64FA24E988024BC5B71E0893E693DE37EC5
9= 5448494E47532E494F
10= 04
22= 0DD06871457933315F
0= 0201061AFF4C000215F7826DA64FA24E988024BC5B71E0893E693DE37EC50A095448494E47532E494F020A040A160DD06871457933315F00000000000000
device foundF7826DA64FA24E988024BC5B71E0893E693DE37E
Type1
new device
1= 06
3= 9FFE
22= 9FFE024672785A74547437576977000001540E46C7B4
-1= E000FA8FCA788707
0= 02010603039FFE17169FFE024672785A74547437576977000001540E46C7B409FFE000FA8FCA788707000000000000000000000000000000000000000000
 

bluedude

Well-Known Member
Licensed User
Hi,

I have tried a few options and the first run looks good. I sometimes see UID and URL for the same beacon but will test it a little more.
 

JeanLC

Member
Licensed User
Hi Erel,

I have an issue withe the BLE2 library while using different devices.
I'm running the BeaconParser_B4A.zip example.

Device 1: Dell Venue 8 / Android 4.4
It scans, show the UUIDs and keep scanning.
If I move out of range beacons are removed as expected in TIME_TO_LIVE_SECONDS.
If I get closer they appear again. Everything OK​
Device 2: LG G3 / Android 5.0
It scans, show the UUIDs and keep scanning.
Then (next scan cycle¿?) the beacons are not found and the UUIDS are removed as TIME_TO_LIVE_SECONDS is reached but with the beacons still available and the Android 4.4 device is still showing the beacons and noticing small distance variations as expected.
Then I set a breakpoint on sub Manager_DeviceFound and run the app again in Debug mode. It stops there during the first scan and as many times as beacons are found. In the "next automatic scan"... nothing happens (The app is responsive and scan circle rotating). If I hit the Stop Scanning and Start again it shows the beacons again and in the "next pass"... nothing.​
What could it be? I have used other scanning apps and they work as intended in both devices.
Thanks in advance.
PD BLE v1.30
 

JeanLC

Member
Licensed User
Seems like your device ignores the "allow duplicates" setting in manager.Scan2 call. Looks like this Android bug: https://code.google.com/p/android/issues/detail?id=65863

Thanks Erel,

After reading the link you sent and others...
Basically the core of this issue is that the BLE standard for scanning was not well defined, leaving the expected behavior open for interpretation. And no way of knowing how the device will respond.

Some devices have firmware that doesn't allow the Allow Duplicate functionality and unless you program for a known device you'll have to Stop and Restart the scan with a period of 1.1 seconds.

After implementing the 1.1 seconds period, I don't think it works so well. I does find again previous beacons but it's really slow and sometimes a found beacon takes to much time to show again updated or even reach the TimeToLive time even at short distance. Maybe it's the bluetooth because I found little difference with this change on the device that was working fine (Dell 4.4 Android), for this one it's better without the 1.1sec but not bad.
I don't know how much overhead its done by doing the 1.1 second Stop Start workaround in Basic instead of directly doing it inside the Jar.
I'll research more.
Thanks again.
 
Last edited:

Marion Opperman

Member
Licensed User
I am receiving incorrect min and maj

Correct is 39273 /32775
I receive -26263 / -32761
can anyone explain to me why this is? I need the values to perform calculations for data
 

luke2012

Well-Known Member
Licensed User
I need to monitor the distance between the phone and the beacon and alert the user if the distance is greather than X meters. Is it possible using this library ?
 

JeanLC

Member
Licensed User
I need to monitor the distance between the phone and the beacon and alert the user if the distance is greather than X meters. Is it possible using this library ?
Yes, you can get RSSI (Power Received by Mobile) and use it to convert to distance based on the value of TX (Calibrated Tx power at 0 m for Eddystone, 1m for iBeacon).
The distance will have different results depending on:
The beacon (antenna) orientation.
Mobile device (antenna) orientation .
Things between Beacon and Mobile device: Walls, people, some furniture, etc.
And of course.... distance.

More beacons... more accuracy and more complex the algorithm.

If you are using it for stolen object or leaving object behind. I think it makes no difference if doing it at 6 meters, 8 meters you walk faster than the mobile device beacon scanning cycle can monitor, unless you want to deplete the battery. Don't forget to alert when no beacon is detected.
You can increase the monitoring frequency if you detect the object is farther than usual but not on the danger distance.

Calibrated Tx power at 0m or 1m could be calibrated to improve distance accuracy (Depending on beacon type).
 
Top