Search in sources :

Example 1 with BeaconParser

use of org.altbeacon.beacon.BeaconParser in project android-beacon-library by AltBeacon.

the class ScanFilterUtils method createScanFiltersForBeaconParsers.

public List<ScanFilter> createScanFiltersForBeaconParsers(List<BeaconParser> beaconParsers) {
    List<ScanFilter> scanFilters = new ArrayList<ScanFilter>();
    // hardware manufacturers
    for (BeaconParser beaconParser : beaconParsers) {
        List<ScanFilterData> sfds = createScanFilterDataForBeaconParser(beaconParser);
        for (ScanFilterData sfd : sfds) {
            ScanFilter.Builder builder = new ScanFilter.Builder();
            if (sfd.serviceUuid != null) {
                // Use a 16 bit service UUID in a 128 bit form
                String serviceUuidString = String.format("0000%04X-0000-1000-8000-00805f9b34fb", sfd.serviceUuid);
                String serviceUuidMaskString = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
                ParcelUuid parcelUuid = ParcelUuid.fromString(serviceUuidString);
                ParcelUuid parcelUuidMask = ParcelUuid.fromString(serviceUuidMaskString);
                if (LogManager.isVerboseLoggingEnabled()) {
                    LogManager.d(TAG, "making scan filter for service: " + serviceUuidString + " " + parcelUuid);
                    LogManager.d(TAG, "making scan filter with service mask: " + serviceUuidMaskString + " " + parcelUuidMask);
                }
                builder.setServiceUuid(parcelUuid, parcelUuidMask);
            } else {
                builder.setServiceUuid(null);
                builder.setManufacturerData((int) sfd.manufacturer, sfd.filter, sfd.mask);
            }
            ScanFilter scanFilter = builder.build();
            if (LogManager.isVerboseLoggingEnabled()) {
                LogManager.d(TAG, "Set up a scan filter: " + scanFilter);
            }
            scanFilters.add(scanFilter);
        }
    }
    return scanFilters;
}
Also used : ParcelUuid(android.os.ParcelUuid) BeaconParser(org.altbeacon.beacon.BeaconParser) ScanFilter(android.bluetooth.le.ScanFilter) ArrayList(java.util.ArrayList)

Example 2 with BeaconParser

use of org.altbeacon.beacon.BeaconParser 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);
    }
}
Also used : ScanSettings(android.bluetooth.le.ScanSettings) BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) ScanFilter(android.bluetooth.le.ScanFilter) BluetoothManager(android.bluetooth.BluetoothManager) ScanFilterUtils(org.altbeacon.beacon.service.scanner.ScanFilterUtils) BeaconParser(org.altbeacon.beacon.BeaconParser) BluetoothAdapter(android.bluetooth.BluetoothAdapter) RequiresApi(androidx.annotation.RequiresApi)

Example 3 with BeaconParser

use of org.altbeacon.beacon.BeaconParser in project android-beacon-library by AltBeacon.

the class SettingsData method apply.

public void apply(@NonNull BeaconService scanService) {
    LogManager.d(TAG, "Applying settings changes to scanner in other process");
    BeaconManager beaconManager = BeaconManager.getInstanceForApplication(scanService);
    List<BeaconParser> beaconParsers = beaconManager.getBeaconParsers();
    boolean beaconParsersChanged = false;
    if (beaconParsers.size() == mBeaconParsers.size()) {
        for (int i = 0; i < beaconParsers.size(); i++) {
            if (!beaconParsers.get(i).equals(mBeaconParsers.get(i))) {
                LogManager.d(TAG, "Beacon parsers have changed to: " + mBeaconParsers.get(i).getLayout());
                beaconParsersChanged = true;
                break;
            }
        }
    } else {
        beaconParsersChanged = true;
        LogManager.d(TAG, "Beacon parsers have been added or removed.");
    }
    if (beaconParsersChanged) {
        LogManager.d(TAG, "Updating beacon parsers");
        beaconManager.getBeaconParsers().clear();
        beaconManager.getBeaconParsers().addAll(mBeaconParsers);
        scanService.reloadParsers();
    } else {
        LogManager.d(TAG, "Beacon parsers unchanged.");
    }
    MonitoringStatus monitoringStatus = MonitoringStatus.getInstanceForApplication(scanService);
    if (monitoringStatus.isStatePreservationOn() && !mRegionStatePersistenceEnabled) {
        monitoringStatus.stopStatusPreservation();
    } else if (!monitoringStatus.isStatePreservationOn() && mRegionStatePersistenceEnabled) {
        monitoringStatus.startStatusPreservation();
    }
    beaconManager.setAndroidLScanningDisabled(mAndroidLScanningDisabled);
    BeaconManager.setRegionExitPeriod(mRegionExitPeriod);
    RangeState.setUseTrackingCache(mUseTrackingCache);
    Beacon.setHardwareEqualityEnforced(mHardwareEqualityEnforced);
}
Also used : BeaconParser(org.altbeacon.beacon.BeaconParser) BeaconManager(org.altbeacon.beacon.BeaconManager)

Example 4 with BeaconParser

use of org.altbeacon.beacon.BeaconParser 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);
}
Also used : BeaconParser(org.altbeacon.beacon.BeaconParser) AltBeaconParser(org.altbeacon.beacon.AltBeaconParser) Identifier(org.altbeacon.beacon.Identifier) ArrayList(java.util.ArrayList) ScanFilterUtils(org.altbeacon.beacon.service.scanner.ScanFilterUtils) Test(org.junit.Test)

Example 5 with BeaconParser

use of org.altbeacon.beacon.BeaconParser 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);
}
Also used : BeaconParser(org.altbeacon.beacon.BeaconParser) AltBeaconParser(org.altbeacon.beacon.AltBeaconParser) ScanFilterUtils(org.altbeacon.beacon.service.scanner.ScanFilterUtils) Test(org.junit.Test)

Aggregations

BeaconParser (org.altbeacon.beacon.BeaconParser)12 ScanFilterUtils (org.altbeacon.beacon.service.scanner.ScanFilterUtils)6 AltBeaconParser (org.altbeacon.beacon.AltBeaconParser)5 Test (org.junit.Test)5 ScanFilter (android.bluetooth.le.ScanFilter)3 ArrayList (java.util.ArrayList)3 ParcelUuid (android.os.ParcelUuid)2 Beacon (org.altbeacon.beacon.Beacon)2 BeaconManager (org.altbeacon.beacon.BeaconManager)2 Region (org.altbeacon.beacon.Region)2 BluetoothAdapter (android.bluetooth.BluetoothAdapter)1 BluetoothManager (android.bluetooth.BluetoothManager)1 BluetoothLeScanner (android.bluetooth.le.BluetoothLeScanner)1 ScanSettings (android.bluetooth.le.ScanSettings)1 MainThread (androidx.annotation.MainThread)1 RequiresApi (androidx.annotation.RequiresApi)1 HashSet (java.util.HashSet)1 Identifier (org.altbeacon.beacon.Identifier)1 DistanceCalculator (org.altbeacon.beacon.distance.DistanceCalculator)1 ModelSpecificDistanceCalculator (org.altbeacon.beacon.distance.ModelSpecificDistanceCalculator)1