Monday, February 12, 2018

An Update to the Ibeacon Scanner App for Android

I have found a time to review the source code of the Ibeacon Scanner App and it would be easy to add the feature to include scanning Eddystone Beacons. To do this, the file DeviceScanActivity.java needs to be modified to detect the 3 Eddystone frames(UID, TLM, URL).

Here is the portion of the file that needs to be modified:

public void onLeScan(final BluetoothDevice device, final int rssi, final byte[]                             scanRecord) {
           int startByte = 2;
           boolean patternFound = false;
           while (startByte <= 5) {
           if ( ((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
                //Identifies correct data length
                ((int) scanRecord[startByte + 3] & 0xff) == 0x15) {
                 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);
       mUUID[mElems]=uuid;
       mRSSI[mElems]= String.valueOf(rssi);
       mElems++;
     //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);
       runOnUiThread(new Runnable() {
     

The variable  patternFound was used to detect the ibeacon pattern and the scanRecord variable currently contains the data being processed and which is being examined whether it contains the ibeacon pattern. And from this, we can already insert the code to identify the current contents of scanRecord if it is an Eddystone pattern.

No comments:

Post a Comment