From ad301ea682fbc4df62144245ddaca570945b98d1 Mon Sep 17 00:00:00 2001 From: "David G. Young" Date: Thu, 9 Jul 2015 21:51:31 -0400 Subject: [PATCH] Only use Andoroid 5 BLE scanning filters in the background --- .../org/altbeacon/beacon/AltBeaconParser.java | 11 ++++++++++- .../scanner/CycledLeScannerForLollipop.java | 7 ++++--- .../org/altbeacon/beacon/BeaconParserTest.java | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/altbeacon/beacon/AltBeaconParser.java b/src/main/java/org/altbeacon/beacon/AltBeaconParser.java index 2e9fcdd23..1d6b49172 100644 --- a/src/main/java/org/altbeacon/beacon/AltBeaconParser.java +++ b/src/main/java/org/altbeacon/beacon/AltBeaconParser.java @@ -41,7 +41,16 @@ public class AltBeaconParser extends BeaconParser { */ public AltBeaconParser() { super(); - mHardwareAssistManufacturers = new int[]{0x0118}; // Radius networks + // Radius networks and other manufacturers seen in AltBeacons + // Note: Other manufacturer codes that have been seen in the wild with AltBeacons are: + // 0x004c, 0x00e0 + // We are not adding these here because there is no indication they are widely used + // for production purposes. We need to keep the hardware assist list short in order to + // save slots. If you are a manufacturer of AltBeacons and want you company code added to + // this list, please open an issue on the Github project for this library. If a beacon + // manufacturer code not in this list is used for AltBeacons, phones using Andoroid 5.x+ + // detection APIs will not be able to detect the beacon in the background. + mHardwareAssistManufacturers = new int[]{0x0118}; this.setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"); } /** diff --git a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java index 38eedff5e..50c8bf1d9 100644 --- a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java +++ b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java @@ -166,6 +166,9 @@ protected void startScan() { if (mBackgroundFlag) { LogManager.d(TAG, "starting scan in SCAN_MODE_LOW_POWER"); settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build(); + filters = new ScanFilterUtils().createScanFiltersForBeaconParsers( + mBeaconManager.getBeaconParsers()); + } else { LogManager.d(TAG, "starting scan in SCAN_MODE_LOW_LATENCY"); settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)).build(); @@ -174,11 +177,9 @@ protected void startScan() { try { if (getScanner() != null) { - List scanFilters = new ScanFilterUtils().createScanFiltersForBeaconParsers( - mBeaconManager.getBeaconParsers()); ScanCallback callback = getNewLeScanCallback(); try { - getScanner().startScan(scanFilters, settings, callback); + getScanner().startScan(filters, settings, callback); } catch (NullPointerException npe) { // Necessary because of https://code.google.com/p/android/issues/detail?id=160503 diff --git a/src/test/java/org/altbeacon/beacon/BeaconParserTest.java b/src/test/java/org/altbeacon/beacon/BeaconParserTest.java index 598157b82..c750d98d9 100644 --- a/src/test/java/org/altbeacon/beacon/BeaconParserTest.java +++ b/src/test/java/org/altbeacon/beacon/BeaconParserTest.java @@ -214,6 +214,22 @@ public void testParseIdentifierThatRunsUnderPduLength() { assertNull("beacon should not be parsed", beacon); } + @Test + public void testParseProblematicBeaconFromIssue229() { + org.robolectric.shadows.ShadowLog.stream = System.err; + + // Note that the length field below is 0x16 instead of 0x1b, indicating that the packet ends + // one byte before the second identifier field starts + + byte[] bytes = hexStringToByteArray("0201061bffe000beac7777772e626c756b692e636f6d000100010001abaa000000"); + BeaconParser parser = new BeaconParser(); + parser.setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"); + + Beacon beacon = parser.fromScanData(bytes, -55, null); + assertNotNull("beacon should be parsed", beacon); + } + + @Test public void testCanParseLocationBeacon() { double latitude = 38.93;