23 April, 2014

How to get BLE overflow hash bit from IOS peripheral?



As we know, when advertising data, the peripheral can only broadcast only one main 128bit service UUID.
When there are more than one 128-bit service UUID or when the app is in the background, we can only get hashed UUIDs from overflow area.
Note: To parse hashed bit, please find the identity: 14ff4c0001
14 => Length
ff => custom ad type
4c00001 => apple defined

//02011a11 067f00000079e3e78e82319dfbf045bdd209084c69676874426c7514ff4c00010000000000000000000000000000000200000000000000000000
List < Byte > byteArrayBytes = new ArrayList < Byte > ();

while (ptr < adv_data.length - 2) {
    byte length = adv_data[ptr++];
    if (length == 0)
        break;

    final int ad_type = adv_data[ptr++] & 0xff;

    switch (ad_type) {
    case 0x02:
    case 0x03:
        break;
    case 0x06:
    case 0x07:
        break;
    case 0xff:
        if (length != 0x14)
            return null;
        // only read the
        ptr = ptr + 4 - 1; // skip the first 4 bytes 14 ff 4c 00 01
        length -= 4; // length skip
        while (length > 0) {
            int value = (adv_data[ptr++] & 0xff);
            length--;
            byteArrayBytes.add((byte) value);

        }
        break;
    default:
        break;
    }
    if (byteArrayBytes.size() == 16)
        break;
    ptr += (length - 1);
}