Search code examples
ibeacon-androideddystone

How to create Eddystone URL Identifiers?


I am using Version 2.5.1 of the Android Beacon Library with the newly added Eddystone support. I am struggling when creating a new Region with a given URL to filter only for Beacons with that URL. For example, I only want to handle Beacons starting with http://example.com

The BeaconParser layout I am using sets the (in my case) fixed-length Scheme+Host+TLD to Id1 and the path to Id2.

How to I create an Identifier like that for an Eddystone URL beacon?

I want to create a new Region like this:

Identifier exampleIdentifier = Identifier.parse("http://example.com");
Region exampleRegion = new Region("Example Region", exampleIdentifier, null, null);

This obviously does not work because the Parser can't handle the given URL. Is this not supported yet or am I completely on the wrong path here?

What worked for me was first converting the Identifier to something that the library understands (i.e. hex notation) and then trying to parse that ( .parse("0x123456..." ). But I am not sure if that's the best and cleanest way to do it.


Solution

  • Eddystone-URL uses a custom compression algorithm to convert the human readable URL to the bytes that get transmitted. The Android Beacon Library includes a utility class that does this compression/decompression for you.

    byte[] urlBytes = UrlBeaconUrlCompressor.compress("http://example.com")
    Identifier identifier = Identifier.fromBytes(urlBytes, 0, urlBytes.length, false);
    

    The first line above generates a byte array that can be converted to an identifier using the second line. See the library docs for more information on working with URL beacons.