Search in sources :

Example 1 with Region

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

the class BeaconService method processRangeData.

private void processRangeData() {
    synchronized (rangedRegionState) {
        for (Region region : rangedRegionState.keySet()) {
            RangeState rangeState = rangedRegionState.get(region);
            LogManager.d(TAG, "Calling ranging callback");
            rangeState.getCallback().call(BeaconService.this, "rangingData", new RangingData(rangeState.finalizeBeacons(), region));
        }
    }
}
Also used : Region(org.altbeacon.beacon.Region)

Example 2 with Region

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

the class ScanHelper method processBeaconFromScan.

/**
 * Helper for processing BLE beacons. This has been extracted from {@link ScanHelper.ScanProcessor} to
 * support simulated scan data for test and debug environments.
 * <p>
 * Processing beacons is a frequent and expensive operation. It should not be run on the main
 * thread to avoid UI contention.
 */
@WorkerThread
private void processBeaconFromScan(@NonNull Beacon beacon) {
    if (Stats.getInstance().isEnabled()) {
        Stats.getInstance().log(beacon);
    }
    if (LogManager.isVerboseLoggingEnabled()) {
        LogManager.d(TAG, "beacon detected : %s", beacon.toString());
    }
    beacon = mExtraDataBeaconTracker.track(beacon);
    // the above
    if (beacon == null) {
        if (LogManager.isVerboseLoggingEnabled()) {
            LogManager.d(TAG, "not processing detections for GATT extra data beacon");
        }
    } else {
        mMonitoringStatus.updateNewlyInsideInRegionsContaining(beacon);
        List<Region> matchedRegions;
        Iterator<Region> matchedRegionIterator;
        LogManager.d(TAG, "looking for ranging region matches for this beacon");
        synchronized (mRangedRegionState) {
            matchedRegions = matchingRegions(beacon, mRangedRegionState.keySet());
            matchedRegionIterator = matchedRegions.iterator();
            while (matchedRegionIterator.hasNext()) {
                Region region = matchedRegionIterator.next();
                LogManager.d(TAG, "matches ranging region: %s", region);
                RangeState rangeState = mRangedRegionState.get(region);
                if (rangeState != null) {
                    rangeState.addBeacon(beacon);
                }
            }
        }
    }
}
Also used : Region(org.altbeacon.beacon.Region) WorkerThread(androidx.annotation.WorkerThread)

Example 3 with Region

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

the class ScanState method applyChanges.

public void applyChanges(BeaconManager beaconManager) {
    mBeaconParsers = new HashSet<>(beaconManager.getBeaconParsers());
    mForegroundScanPeriod = beaconManager.getForegroundScanPeriod();
    mForegroundBetweenScanPeriod = beaconManager.getForegroundBetweenScanPeriod();
    mBackgroundScanPeriod = beaconManager.getBackgroundScanPeriod();
    mBackgroundBetweenScanPeriod = beaconManager.getBackgroundBetweenScanPeriod();
    mBackgroundMode = beaconManager.getBackgroundMode();
    ArrayList<Region> existingMonitoredRegions = new ArrayList<>(mMonitoringStatus.regions());
    ArrayList<Region> existingRangedRegions = new ArrayList<>(mRangedRegionState.keySet());
    ArrayList<Region> newMonitoredRegions = new ArrayList<>(beaconManager.getMonitoredRegions());
    ArrayList<Region> newRangedRegions = new ArrayList<>(beaconManager.getRangedRegions());
    LogManager.d(TAG, "ranged regions: old=" + existingRangedRegions.size() + " new=" + newRangedRegions.size());
    LogManager.d(TAG, "monitored regions: old=" + existingMonitoredRegions.size() + " new=" + newMonitoredRegions.size());
    for (Region newRangedRegion : newRangedRegions) {
        if (!existingRangedRegions.contains(newRangedRegion)) {
            LogManager.d(TAG, "Starting ranging region: " + newRangedRegion);
            mRangedRegionState.put(newRangedRegion, new RangeState(new Callback(mContext.getPackageName())));
        }
    }
    for (Region existingRangedRegion : existingRangedRegions) {
        if (!newRangedRegions.contains(existingRangedRegion)) {
            LogManager.d(TAG, "Stopping ranging region: " + existingRangedRegion);
            mRangedRegionState.remove(existingRangedRegion);
        }
    }
    LogManager.d(TAG, "Updated state with " + newRangedRegions.size() + " ranging regions and " + newMonitoredRegions.size() + " monitoring regions.");
    this.save();
}
Also used : ArrayList(java.util.ArrayList) Region(org.altbeacon.beacon.Region)

Example 4 with Region

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

the class RangingDataTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    Context context = RuntimeEnvironment.application;
    ArrayList<Identifier> identifiers = new ArrayList<Identifier>();
    identifiers.add(Identifier.parse("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6"));
    identifiers.add(Identifier.parse("1"));
    identifiers.add(Identifier.parse("2"));
    Region region = new Region("testRegion", identifiers);
    ArrayList<Beacon> beacons = new ArrayList<Beacon>();
    Beacon beacon = new Beacon.Builder().setIdentifiers(identifiers).setRssi(-1).setRunningAverageRssi(-2).setTxPower(-50).setBluetoothAddress("01:02:03:04:05:06").build();
    beacon.setRssiMeasurementCount(1);
    beacon.setPacketCount(2);
    for (int i = 0; i < 10; i++) {
        beacons.add(beacon);
    }
    RangingData data = new RangingData(beacons, region);
    Bundle bundle = data.toBundle();
    RangingData data2 = RangingData.fromBundle(bundle);
    assertEquals("beacon count shouild be restored", 10, data2.getBeacons().size());
    assertEquals("region identifier 1 shouild be restored", "2f234454-cf6d-4a0f-adf2-f4911ba9ffa6", data2.getRegion().getId1().toString());
    Beacon restoredBeacon = data2.getBeacons().iterator().next();
    assertEquals("beacon identifier 1 shouild be restored", "2f234454-cf6d-4a0f-adf2-f4911ba9ffa6", restoredBeacon.getId1().toString());
    assertEquals("RSSI is restored", -1, restoredBeacon.getRssi());
    assertEquals("Average RSSI is restored", -2.0, restoredBeacon.getRunningAverageRssi(), 0.0);
    assertEquals("TXPower is restored", -50, restoredBeacon.getTxPower());
    assertEquals("Measurement count is restored", 1, restoredBeacon.getMeasurementCount());
    assertEquals("Packet count is restored", 2, restoredBeacon.getPacketCount());
}
Also used : Context(android.content.Context) Identifier(org.altbeacon.beacon.Identifier) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) Beacon(org.altbeacon.beacon.Beacon) Region(org.altbeacon.beacon.Region) Test(org.junit.Test)

Example 5 with Region

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

the class MonitoringStatusTest method refusesToRestoreRegionsIfTooMuchTimeHasPassedSinceSavingTest.

@Test
public void refusesToRestoreRegionsIfTooMuchTimeHasPassedSinceSavingTest() throws Exception {
    Context context = RuntimeEnvironment.application;
    MonitoringStatus monitoringStatus = new MonitoringStatus(context);
    for (int i = 0; i < 50; i++) {
        Region region = new Region("" + i, null, null, null);
        monitoringStatus.addRegion(region, null);
    }
    monitoringStatus.saveMonitoringStatusIfOn();
    // Set update time to one hour ago
    monitoringStatus.updateMonitoringStatusTime(System.currentTimeMillis() - 1000 * 3600l);
    MonitoringStatus monitoringStatus2 = new MonitoringStatus(context);
    assertEquals("restored regions should be none", 0, monitoringStatus2.regions().size());
}
Also used : Context(android.content.Context) Region(org.altbeacon.beacon.Region) Test(org.junit.Test)

Aggregations

Region (org.altbeacon.beacon.Region)23 Context (android.content.Context)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 Beacon (org.altbeacon.beacon.Beacon)4 Bundle (android.os.Bundle)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BeaconManager (org.altbeacon.beacon.BeaconManager)2 BeaconParser (org.altbeacon.beacon.BeaconParser)2 Identifier (org.altbeacon.beacon.Identifier)2 ScanFilter (android.bluetooth.le.ScanFilter)1 ParcelUuid (android.os.ParcelUuid)1 RemoteException (android.os.RemoteException)1 MainThread (androidx.annotation.MainThread)1 WorkerThread (androidx.annotation.WorkerThread)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InvalidClassException (java.io.InvalidClassException)1