Skip to content

Instantly share code, notes, and snippets.

@mikklfr
Created March 5, 2015 15:30
Show Gist options
  • Select an option

  • Save mikklfr/0648b6bb74d1e7d3c9dd to your computer and use it in GitHub Desktop.

Select an option

Save mikklfr/0648b6bb74d1e7d3c9dd to your computer and use it in GitHub Desktop.

Revisions

  1. mikklfr created this gist Mar 5, 2015.
    45 changes: 45 additions & 0 deletions uuid
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    static final char[] hexArray = "0123456789ABCDEF".toCharArray();
    private static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    for ( int j = 0; j < bytes.length; j++ ) {
    int v = bytes[j] & 0xFF;
    hexChars[j * 2] = hexArray[v >>> 4];
    hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
    }

    private String uuidWithRecord(byte[] scanRecord) {
    int startByte = 2;
    boolean patternFound = false;
    while (startByte <= 5) {
    if ( ((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
    ((int) scanRecord[startByte + 3] & 0xff) == 0x15) { //Identifies correct data length
    patternFound = true;
    break;
    }
    startByte++;
    }

    if (patternFound) {
    //Convert to hex String
    byte[] uuidBytes = new byte[16];
    System.arraycopy(scanRecord, startByte+4, uuidBytes, 0, 16);
    String hexString = bytesToHex(uuidBytes);

    //Here is your UUID
    String uuid = hexString.substring(0,8) + "-" +
    hexString.substring(8,12) + "-" +
    hexString.substring(12,16) + "-" +
    hexString.substring(16,20) + "-" +
    hexString.substring(20,32);

    //Here is your Major value
    int major = (scanRecord[startByte+20] & 0xff) * 0x100 + (scanRecord[startByte+21] & 0xff);

    //Here is your Minor value
    int minor = (scanRecord[startByte+22] & 0xff) * 0x100 + (scanRecord[startByte+23] & 0xff);
    return uuid;
    }
    return null;
    }