use of org.altbeacon.beacon.service.scanner.ScanFilterUtils in project android-beacon-library by AltBeacon.
the class ScanHelper method startAndroidOBackgroundScan.
@RequiresApi(api = Build.VERSION_CODES.O)
void startAndroidOBackgroundScan(Set<BeaconParser> beaconParsers, List<Region> regions) {
ScanSettings settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build();
List<ScanFilter> filters = new ScanFilterUtils().createScanFiltersForBeaconParsers(new ArrayList<BeaconParser>(beaconParsers), regions);
try {
final BluetoothManager bluetoothManager = (BluetoothManager) mContext.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null) {
LogManager.w(TAG, "Failed to construct a BluetoothAdapter");
} else if (!bluetoothAdapter.isEnabled()) {
LogManager.w(TAG, "Failed to start background scan on Android O: BluetoothAdapter is not enabled");
} else {
BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
if (scanner != null) {
int result = scanner.startScan(filters, settings, getScanCallbackIntent());
if (result != 0) {
LogManager.e(TAG, "Failed to start background scan on Android O. Code: " + result);
} else {
LogManager.d(TAG, "Started passive beacon scan");
}
} else {
LogManager.e(TAG, "Failed to start background scan on Android O: scanner is null");
}
}
} catch (SecurityException e) {
LogManager.e(TAG, "SecurityException making Android O background scanner");
} catch (NullPointerException e) {
// Needed to stop a crash caused by internal NPE thrown by Android. See issue #636
LogManager.e(TAG, "NullPointerException starting Android O background scanner", e);
} catch (RuntimeException e) {
// Needed to stop a crash caused by internal Android throw. See issue #701
LogManager.e(TAG, "Unexpected runtime exception starting Android O background scanner", e);
}
}
use of org.altbeacon.beacon.service.scanner.ScanFilterUtils in project android-beacon-library by AltBeacon.
the class ScanFilterUtilsTest method testScanFilterWithIdentifiers.
@Test
public void testScanFilterWithIdentifiers() throws Exception {
org.robolectric.shadows.ShadowLog.stream = System.err;
BeaconParser parser = new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
parser.setHardwareAssistManufacturerCodes(new int[] { 0x004c });
// no manifest available in robolectric
BeaconManager.setManifestCheckingDisabled(true);
ArrayList<Identifier> identifiers = new ArrayList<Identifier>();
identifiers.add(Identifier.parse("2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"));
identifiers.add(Identifier.parse("0x0102"));
identifiers.add(Identifier.parse("0x0304"));
List<ScanFilterUtils.ScanFilterData> scanFilterDatas = new ScanFilterUtils().createScanFilterDataForBeaconParser(parser, identifiers);
assertEquals("scanFilters should be of correct size", 1, scanFilterDatas.size());
ScanFilterUtils.ScanFilterData sfd = scanFilterDatas.get(0);
assertEquals("manufacturer should be right", 0x004c, sfd.manufacturer);
assertEquals("mask length should be right", 22, sfd.mask.length);
assertArrayEquals("mask should be right", new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff }, sfd.mask);
assertArrayEquals("filter should be right", new byte[] { (byte) 0x02, (byte) 0x15, (byte) 0x2F, (byte) 0x23, (byte) 0x44, (byte) 0x54, (byte) 0xCF, (byte) 0x6D, (byte) 0x4A, (byte) 0x0F, (byte) 0xAD, (byte) 0xF2, (byte) 0xF4, (byte) 0x91, (byte) 0x1B, (byte) 0xA9, (byte) 0xFF, (byte) 0xA6, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 }, sfd.filter);
}
use of org.altbeacon.beacon.service.scanner.ScanFilterUtils in project android-beacon-library by AltBeacon.
the class ScanFilterUtilsTest method testZeroOffsetScanFilter.
@Test
public void testZeroOffsetScanFilter() throws Exception {
org.robolectric.shadows.ShadowLog.stream = System.err;
BeaconParser parser = new BeaconParser();
parser.setBeaconLayout("m:0-3=11223344,i:4-6,p:24-24");
// no manifest available in robolectric
BeaconManager.setManifestCheckingDisabled(true);
List<ScanFilterUtils.ScanFilterData> scanFilterDatas = new ScanFilterUtils().createScanFilterDataForBeaconParser(parser, null);
assertEquals("scanFilters should be of correct size", 1, scanFilterDatas.size());
ScanFilterUtils.ScanFilterData sfd = scanFilterDatas.get(0);
assertEquals("manufacturer should be right", 0x004c, sfd.manufacturer);
assertEquals("mask length should be right", 2, sfd.mask.length);
assertArrayEquals("mask should be right", new byte[] { (byte) 0xff, (byte) 0xff }, sfd.mask);
assertArrayEquals("filter should be right", new byte[] { (byte) 0x33, (byte) 0x44 }, sfd.filter);
}
use of org.altbeacon.beacon.service.scanner.ScanFilterUtils in project android-beacon-library by AltBeacon.
the class ScanFilterUtilsTest method testEddystoneScanFilterData.
@Test
public void testEddystoneScanFilterData() throws Exception {
org.robolectric.shadows.ShadowLog.stream = System.err;
BeaconParser parser = new BeaconParser();
parser.setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT);
// no manifest available in robolectric
BeaconManager.setManifestCheckingDisabled(true);
List<ScanFilterUtils.ScanFilterData> scanFilterDatas = new ScanFilterUtils().createScanFilterDataForBeaconParser(parser, null);
assertEquals("scanFilters should be of correct size", 1, scanFilterDatas.size());
ScanFilterUtils.ScanFilterData sfd = scanFilterDatas.get(0);
assertEquals("serviceUuid should be right", new Long(0xfeaa), sfd.serviceUuid);
}
use of org.altbeacon.beacon.service.scanner.ScanFilterUtils in project android-beacon-library by AltBeacon.
the class ScanFilterUtilsTest method testGetAltBeaconScanFilter.
@Test
public void testGetAltBeaconScanFilter() throws Exception {
org.robolectric.shadows.ShadowLog.stream = System.err;
BeaconParser parser = new AltBeaconParser();
// no manifest available in robolectric
BeaconManager.setManifestCheckingDisabled(true);
List<ScanFilterUtils.ScanFilterData> scanFilterDatas = new ScanFilterUtils().createScanFilterDataForBeaconParser(parser, null);
assertEquals("scanFilters should be of correct size", 1, scanFilterDatas.size());
ScanFilterUtils.ScanFilterData sfd = scanFilterDatas.get(0);
assertEquals("manufacturer should be right", 0x0118, sfd.manufacturer);
assertEquals("mask length should be right", 2, sfd.mask.length);
assertArrayEquals("mask should be right", new byte[] { (byte) 0xff, (byte) 0xff }, sfd.mask);
assertArrayEquals("filter should be right", new byte[] { (byte) 0xbe, (byte) 0xac }, sfd.filter);
}
Aggregations