Search code examples
androidibeaconaltbeaconpayload

Android altbeacon Library - How to access complete payload of scanned Beacon


How can I access the raw payload data of a Scanned Beacon using the altbeacon library?


Solution

  • The Android Beacon Library is not designed for this purpose, so the APIs to do what you want are a little awkward.

    Upon detection the library automatically parses payload and converts the bytes to a beacon object. But because the library also supports transmission it has utilities to convert a beacon object right back to bytes. If using iBeacon, you can convert a detected beacon back to raw bytes like this:

    BeaconParser iBeaconParser = new BeaconParser()
        .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
    byte[] payloadBytes = iBeaconParser.getBeaconAdvertisementData(beacon);  
    

    In general, this is a lossless process, as beacon formats typically use up every byte of the payload. In the unlikely event that you have a custom beacon format that does not use all the bytes, you would need to alter the format slightly to add extra data fields at the end so that the full payload is parsed into the beacon. Otherwise you will lose these extra bytes in the conversion.