-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into separate-process-service-support
- Loading branch information
Showing
5 changed files
with
157 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/org/altbeacon/beacon/service/scanner/DistinctPacketDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.altbeacon.beacon.service.scanner; | ||
|
||
import android.util.Log; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Created by dyoung on 4/8/17. | ||
* | ||
* This class tracks whether multiple distinct BLE packets have been seen, with the purpose of | ||
* determining if the Android device supports detecting multiple distinct packets in a single scan. | ||
* Some older devices are not capable of this (e.g. Nexus 4, Moto G1), so detecting multiple packets | ||
* requires stopping and restarting scanning on these devices. This allows detecting if that is | ||
* neessary | ||
*/ | ||
public class DistinctPacketDetector { | ||
// Sanity limit for the number of packets to track, so we don't use too much memory | ||
private static final int MAX_PACKETS_TO_TRACK = 1000; | ||
protected Set<ByteBuffer> mDistinctPacketsDetected = new HashSet<ByteBuffer>(); | ||
|
||
public void clearDetections() { | ||
mDistinctPacketsDetected.clear(); | ||
} | ||
|
||
public boolean isPacketDistinct(String originMacAddress, byte[] scanRecord) { | ||
byte[] macBytes = originMacAddress.getBytes(); | ||
ByteBuffer buffer = ByteBuffer.allocate(macBytes.length+scanRecord.length); | ||
buffer.put(macBytes); | ||
buffer.put(scanRecord); | ||
buffer.rewind(); // rewind puts position back to beginning so .equals and .hashCode work | ||
|
||
boolean distinct = !mDistinctPacketsDetected.contains(buffer); | ||
if (mDistinctPacketsDetected.size() == MAX_PACKETS_TO_TRACK) { | ||
return mDistinctPacketsDetected.contains(buffer); | ||
} | ||
else { | ||
return mDistinctPacketsDetected.add(buffer); | ||
} | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
src/test/java/org/altbeacon/beacon/service/scanner/DistinctPacketDetectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.altbeacon.beacon.service.scanner; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
@Config(sdk = 18) | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class DistinctPacketDetectorTest { | ||
@BeforeClass | ||
public static void testSetup() { | ||
} | ||
|
||
@AfterClass | ||
public static void testCleanup() { | ||
|
||
} | ||
|
||
@Test | ||
public void testSecondDuplicatePacketIsNotDistinct() throws Exception { | ||
DistinctPacketDetector dpd = new DistinctPacketDetector(); | ||
dpd.isPacketDistinct("01:02:03:04:05:06", new byte[] {0x01, 0x02}); | ||
boolean secondResult = dpd.isPacketDistinct("01:02:03:04:05:06", new byte[] {0x01, 0x02}); | ||
assertFalse("second call with same packet should not be distinct", secondResult); | ||
} | ||
|
||
@Test | ||
public void testSecondNonDuplicatePacketIsDistinct() throws Exception { | ||
DistinctPacketDetector dpd = new DistinctPacketDetector(); | ||
dpd.isPacketDistinct("01:02:03:04:05:06", new byte[] {0x01, 0x02}); | ||
boolean secondResult = dpd.isPacketDistinct("01:02:03:04:05:06", new byte[] {0x03, 0x04}); | ||
assertTrue("second call with different packet should be distinct", secondResult); | ||
} | ||
|
||
@Test | ||
public void testSamePacketForDifferentMacIsDistinct() throws Exception { | ||
DistinctPacketDetector dpd = new DistinctPacketDetector(); | ||
dpd.isPacketDistinct("01:02:03:04:05:06", new byte[] {0x01, 0x02}); | ||
boolean secondResult = dpd.isPacketDistinct("01:01:01:01:01:01", new byte[] {0x01, 0x02}); | ||
assertTrue("second packet with different mac should be distinct", secondResult); | ||
} | ||
|
||
@Test | ||
public void clearingDetectionsPreventsDistinctDetection() throws Exception { | ||
DistinctPacketDetector dpd = new DistinctPacketDetector(); | ||
dpd.isPacketDistinct("01:02:03:04:05:06", new byte[] {0x01, 0x02}); | ||
dpd.clearDetections(); | ||
boolean secondResult = dpd.isPacketDistinct("01:02:03:04:05:06", new byte[] {0x01, 0x02}); | ||
assertTrue("second call with same packet after clear should be distinct", secondResult); | ||
} | ||
|
||
} |