Search in sources :

Example 11 with BeaconParser

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

the class ScanFilterUtilsTest method testGenericScanFilter.

@Test
public void testGenericScanFilter() throws Exception {
    org.robolectric.shadows.ShadowLog.stream = System.err;
    BeaconParser parser = new BeaconParser();
    parser.setBeaconLayout("m:2-3=1111,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) 0x11, (byte) 0x11 }, sfd.filter);
    assertNull("serviceUuid should be null", sfd.serviceUuid);
}
Also used : BeaconParser(org.altbeacon.beacon.BeaconParser) AltBeaconParser(org.altbeacon.beacon.AltBeaconParser) ScanFilterUtils(org.altbeacon.beacon.service.scanner.ScanFilterUtils) Test(org.junit.Test)

Example 12 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<Region> regions) {
    ArrayList<Region> nonNullRegions = new ArrayList<>();
    if (regions == null) {
        nonNullRegions.add(null);
    } else {
        nonNullRegions.addAll(regions);
    }
    List<ScanFilter> scanFilters = new ArrayList<ScanFilter>();
    for (Region region : nonNullRegions) {
        for (BeaconParser beaconParser : beaconParsers) {
            List<ScanFilterData> sfds = createScanFilterDataForBeaconParser(beaconParser, region == null ? null : region.getIdentifiers());
            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 if (sfd.serviceUuid128Bit.length != 0) {
                    String serviceUuidString = Identifier.fromBytes(sfd.serviceUuid128Bit, 0, 16, true).toString();
                    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);
                    if (LogManager.isVerboseLoggingEnabled()) {
                        LogManager.d(TAG, "making scan filter for manufacturer: " + sfd.manufacturer + " " + sfd.filter);
                    }
                }
                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) Region(org.altbeacon.beacon.Region)

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