diff --git a/library/src/main/java/uk/co/alt236/bluetoothlelib/util/IBeaconUtils.java b/library/src/main/java/uk/co/alt236/bluetoothlelib/util/IBeaconUtils.java index 084f15a..cadc2c9 100644 --- a/library/src/main/java/uk/co/alt236/bluetoothlelib/util/IBeaconUtils.java +++ b/library/src/main/java/uk/co/alt236/bluetoothlelib/util/IBeaconUtils.java @@ -8,7 +8,7 @@ public class IBeaconUtils { private static final double DISTANCE_THRESHOLD_IMMEDIATE = 0.5; private static final double DISTANCE_THRESHOLD_NEAR = 3.0; - private static final byte[] MANUFACTURER_DATA_IBEACON_PREFIX = new byte[]{0x4C, 0x00, 0x02, 0x15}; + private static final byte[] MANUFACTURER_DATA_IBEACON_PREFIX = {0x4C, 0x00, 0x02, 0x15}; /** * Calculates the accuracy of an RSSI reading. @@ -103,7 +103,7 @@ public class IBeaconUtils { * @return true if the device is an iBeacon, false otherwise */ public static boolean isThisAnIBeacon(final BluetoothLeDevice device) { - return isThisAnIBeacon( - device.getAdRecordStore().getRecordDataAsString(AdRecord.TYPE_MANUFACTURER_SPECIFIC_DATA).getBytes()); + final int key = AdRecord.TYPE_MANUFACTURER_SPECIFIC_DATA; + return isThisAnIBeacon(device.getAdRecordStore().getRecordDataAsString(key).getBytes()); } } diff --git a/library/src/test/java/uk/co/alt236/bluetoothlelib/util/IBeaconUtilsTest.java b/library/src/test/java/uk/co/alt236/bluetoothlelib/util/IBeaconUtilsTest.java new file mode 100644 index 0000000..8253383 --- /dev/null +++ b/library/src/test/java/uk/co/alt236/bluetoothlelib/util/IBeaconUtilsTest.java @@ -0,0 +1,39 @@ +package uk.co.alt236.bluetoothlelib.util; + +import junit.framework.TestCase; + +/** + * + */ +public class IBeaconUtilsTest extends TestCase { + + public void testIsThisAnIBeacon() throws Exception { + assertFalse(IBeaconUtils.isThisAnIBeacon((byte[]) null)); + assertFalse(IBeaconUtils.isThisAnIBeacon(new byte[0])); + assertFalse(IBeaconUtils.isThisAnIBeacon(new byte[25])); + + assertTrue(IBeaconUtils.isThisAnIBeacon(new byte[]{ + 0x4C, 0x00, 0x02, 0x15, 0x00, // <- Magic iBeacon header + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00 + })); + } + + public void testGetDistanceDescriptor() throws Exception { + assertEquals(IBeaconDistanceDescriptor.UNKNOWN, IBeaconUtils.getDistanceDescriptor(-1)); + + assertEquals(IBeaconDistanceDescriptor.IMMEDIATE, IBeaconUtils.getDistanceDescriptor(0)); + assertEquals(IBeaconDistanceDescriptor.IMMEDIATE, IBeaconUtils.getDistanceDescriptor(0.4)); + + assertEquals(IBeaconDistanceDescriptor.NEAR, IBeaconUtils.getDistanceDescriptor(0.5)); + assertEquals(IBeaconDistanceDescriptor.NEAR, IBeaconUtils.getDistanceDescriptor(2.9)); + + assertEquals(IBeaconDistanceDescriptor.FAR, IBeaconUtils.getDistanceDescriptor(3)); + } + + public void testCalculateUuidString() throws Exception { + + } +} \ No newline at end of file