Made the iBeacon check more generic

This commit is contained in:
Alexandros Schillings
2014-03-20 18:54:03 +00:00
parent 680a630244
commit 9cd3b20899
@@ -1,14 +1,14 @@
package uk.co.alt236.bluetoothlelib.util; package uk.co.alt236.bluetoothlelib.util;
import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice; import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
import uk.co.alt236.bluetoothlelib.device.adrecord.AdRecord;
public class IBeaconUtils { public class IBeaconUtils {
private static final double DISTANCE_THRESHOLD_WTF = 0.0; private static final double DISTANCE_THRESHOLD_WTF = 0.0;
private static final double DISTANCE_THRESHOLD_IMMEDIATE = 0.5; private static final double DISTANCE_THRESHOLD_IMMEDIATE = 0.5;
private static final double DISTANCE_THRESHOLD_NEAR = 3.0; private static final double DISTANCE_THRESHOLD_NEAR = 3.0;
private static final byte[] SCAN_RECORD_PREFIX_IBEACON_1 = new byte[]{0x02, 0x01, 0x1A, 0x1A, (byte) 0xFF, 0x4C, 0x00, 0x02, 0x15}; private static final byte[] MANUFACTURER_DATA_IBEACON_PREFIX = new byte[]{0x4C, 0x00, 0x02, 0x15};
private static final byte[] SCAN_RECORD_PREFIX_IBEACON_2 = new byte[]{0x02, 0x01, 0x06, 0x1A, (byte) 0xFF, 0x4C, 0x00, 0x02, 0x15};
public static IBeaconDistanceDescriptor getDistanceDescriptor(double accuracy){ public static IBeaconDistanceDescriptor getDistanceDescriptor(double accuracy){
if(accuracy < DISTANCE_THRESHOLD_WTF){ if(accuracy < DISTANCE_THRESHOLD_WTF){
@@ -57,21 +57,23 @@ public class IBeaconUtils {
* @return * @return
*/ */
public static boolean isThisAnIBeacon(BluetoothLeDevice device){ public static boolean isThisAnIBeacon(BluetoothLeDevice device){
return isThisAnIBeacon(device.getScanRecord()); return isThisAnIBeacon(
device.getAdRecordStore().getRecordDataAsString(AdRecord.TYPE_MANUFACTURER_SPECIFIC_DATA).getBytes());
} }
/** /**
* Ascertains whether a scanRecord bytearray belongs to an iBeacon; * Ascertains whether a Manufacturer Data byte array belongs to an iBeacon;
* *
* @param scanRecord a Bluetooth LE device's scanRecord. * @param scanRecord a Bluetooth LE device's manufacturerData.
* @return * @return
*/ */
public static boolean isThisAnIBeacon(byte[] scanRecord){ public static boolean isThisAnIBeacon(byte[] manufacturerData){
if(ByteUtils.doesArrayBeginWith(scanRecord, SCAN_RECORD_PREFIX_IBEACON_1)){ if(manufacturerData == null){return false;}
return true;
}
if(ByteUtils.doesArrayBeginWith(scanRecord, SCAN_RECORD_PREFIX_IBEACON_2)){ // An iBeacon record must be at least 25 chars long
if(!(manufacturerData.length >= 25)){return false;}
if(ByteUtils.doesArrayBeginWith(manufacturerData, MANUFACTURER_DATA_IBEACON_PREFIX)){
return true; return true;
} }