Skip to content

Commit 33f2a26

Browse files
committed
Added some IBeaconUtils tests
1 parent 70169dc commit 33f2a26

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

library/src/main/java/uk/co/alt236/bluetoothlelib/util/IBeaconUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class IBeaconUtils {
88
private static final double DISTANCE_THRESHOLD_IMMEDIATE = 0.5;
99
private static final double DISTANCE_THRESHOLD_NEAR = 3.0;
1010

11-
private static final byte[] MANUFACTURER_DATA_IBEACON_PREFIX = new byte[]{0x4C, 0x00, 0x02, 0x15};
11+
private static final byte[] MANUFACTURER_DATA_IBEACON_PREFIX = {0x4C, 0x00, 0x02, 0x15};
1212

1313
/**
1414
* Calculates the accuracy of an RSSI reading.
@@ -103,7 +103,7 @@ public static boolean isThisAnIBeacon(final byte[] manufacturerData) {
103103
* @return true if the device is an iBeacon, false otherwise
104104
*/
105105
public static boolean isThisAnIBeacon(final BluetoothLeDevice device) {
106-
return isThisAnIBeacon(
107-
device.getAdRecordStore().getRecordDataAsString(AdRecord.TYPE_MANUFACTURER_SPECIFIC_DATA).getBytes());
106+
final int key = AdRecord.TYPE_MANUFACTURER_SPECIFIC_DATA;
107+
return isThisAnIBeacon(device.getAdRecordStore().getRecordDataAsString(key).getBytes());
108108
}
109109
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package uk.co.alt236.bluetoothlelib.util;
2+
3+
import junit.framework.TestCase;
4+
5+
/**
6+
*
7+
*/
8+
public class IBeaconUtilsTest extends TestCase {
9+
10+
public void testIsThisAnIBeacon() throws Exception {
11+
assertFalse(IBeaconUtils.isThisAnIBeacon((byte[]) null));
12+
assertFalse(IBeaconUtils.isThisAnIBeacon(new byte[0]));
13+
assertFalse(IBeaconUtils.isThisAnIBeacon(new byte[25]));
14+
15+
assertTrue(IBeaconUtils.isThisAnIBeacon(new byte[]{
16+
0x4C, 0x00, 0x02, 0x15, 0x00, // <- Magic iBeacon header
17+
0x00, 0x00, 0x00, 0x00, 0x00,
18+
0x00, 0x00, 0x00, 0x00, 0x00,
19+
0x00, 0x00, 0x00, 0x00, 0x00,
20+
0x00, 0x00, 0x00, 0x00, 0x00
21+
}));
22+
}
23+
24+
public void testGetDistanceDescriptor() throws Exception {
25+
assertEquals(IBeaconDistanceDescriptor.UNKNOWN, IBeaconUtils.getDistanceDescriptor(-1));
26+
27+
assertEquals(IBeaconDistanceDescriptor.IMMEDIATE, IBeaconUtils.getDistanceDescriptor(0));
28+
assertEquals(IBeaconDistanceDescriptor.IMMEDIATE, IBeaconUtils.getDistanceDescriptor(0.4));
29+
30+
assertEquals(IBeaconDistanceDescriptor.NEAR, IBeaconUtils.getDistanceDescriptor(0.5));
31+
assertEquals(IBeaconDistanceDescriptor.NEAR, IBeaconUtils.getDistanceDescriptor(2.9));
32+
33+
assertEquals(IBeaconDistanceDescriptor.FAR, IBeaconUtils.getDistanceDescriptor(3));
34+
}
35+
36+
public void testCalculateUuidString() throws Exception {
37+
38+
}
39+
}

0 commit comments

Comments
 (0)