Пример #1
0
 /**
  * Obtains BLE advertisement data and checks if it is an iBeacon
  *
  * @param data the advertisement data
  * @return the IBeacon found or null if not an iBeacon
  */
 private IBeacon parseAdvertisementData(byte[] data) {
   Log.i(Utils.LOG_TAG, Arrays.toString(data));
   // First, check the prefix for our beacons
   if (data[0] == 0x02
       && data[1] == 0x01
       && data[4] == (byte) 0xFF
       && data[7] == 0x02) { // iBeacon candidate
     byte[] uuid =
         Arrays.copyOfRange(data, ADV_PREFIX_LENGTH, ADV_PREFIX_LENGTH + ADV_UUID_LENGTH);
     // Now filter beacons based on UUID if any
     if (_uuid == null || Arrays.equals(_uuid, uuid)) {
       int offset = ADV_PREFIX_LENGTH + ADV_UUID_LENGTH;
       IBeacon ibeacon = new IBeacon();
       ibeacon.setUuid(uuid);
       int major = ((data[offset] << 8) & 0x0000ff00) | (data[offset + 1] & 0x000000ff);
       ibeacon.setMajor(major);
       int minor = ((data[offset + 2] << 8) & 0x0000ff00) | (data[offset + 3] & 0x000000ff);
       ibeacon.setMinor(minor);
       ibeacon.setPowerValue(data[offset + 4]);
       return ibeacon;
     }
   }
   return null;
 }