org.altbeacon.beacon.Region Java Examples
The following examples show how to use
org.altbeacon.beacon.Region.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RuuviScannerApplication.java From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void onCreate() { super.onCreate(); me = this; Log.d(TAG, "App class onCreate"); FlowManager.init(getApplicationContext()); prefs = new Preferences(getApplicationContext()); ruuviRangeNotifier = new RuuviRangeNotifier(getApplicationContext(), "RuuviScannerApplication"); Foreground.init(this); Foreground.get().addListener(listener); new Handler().postDelayed(new Runnable() { @Override public void run() { if (!foreground) { if (prefs.getBackgroundScanMode() == BackgroundScanModes.FOREGROUND) { new ServiceUtils(getApplicationContext()).startForegroundService(); } else if (prefs.getBackgroundScanMode() == BackgroundScanModes.BACKGROUND) { startBackgroundScanning(); } } } }, 5000); region = new Region("com.ruuvi.station.leRegion", null, null, null); }
Example #2
Source File: MonitoringStatus.java From android-beacon-library with Apache License 2.0 | 6 votes |
public synchronized void updateNewlyOutside() { Iterator<Region> monitoredRegionIterator = regions().iterator(); boolean needsMonitoringStateSaving = false; while (monitoredRegionIterator.hasNext()) { Region region = monitoredRegionIterator.next(); RegionMonitoringState state = stateOf(region); if (state.markOutsideIfExpired()) { needsMonitoringStateSaving = true; LogManager.d(TAG, "found a monitor that expired: %s", region); state.getCallback().call(mContext, "monitoringData", new MonitoringData(state.getInside(), region).toBundle()); } } if (needsMonitoringStateSaving) { saveMonitoringStatusIfOn(); } else { updateMonitoringStatusTime(System.currentTimeMillis()); } }
Example #3
Source File: RxBeacon.java From RxBeacon with Apache License 2.0 | 6 votes |
public Observable<RxBeaconRange> beaconsInRegion() { return startup() .flatMap(new Function<Boolean, ObservableSource<RxBeaconRange>>() { @Override public ObservableSource<RxBeaconRange> apply(@NonNull Boolean aBoolean) throws Exception { return Observable.create(new ObservableOnSubscribe<RxBeaconRange>() { @Override public void subscribe(@NonNull final ObservableEmitter<RxBeaconRange> objectObservableEmitter) throws Exception { beaconManager.addRangeNotifier(new RangeNotifier() { @Override public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) { objectObservableEmitter.onNext(new RxBeaconRange(collection, region)); } }); beaconManager.startRangingBeaconsInRegion(getRegion()); } }); } }); }
Example #4
Source File: BeaconManager.java From PresencePublisher with MIT License | 6 votes |
public synchronized void removeBeacon(Context context, String beaconId) { Context applicationContext = context.getApplicationContext(); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext); Set<String> storedBeacons = new HashSet<>(sharedPreferences.getStringSet(BEACON_LIST, Collections.emptySet())); storedBeacons.remove(beaconId); Set<String> foundBeacons = new HashSet<>(sharedPreferences.getStringSet(FOUND_BEACON_LIST, Collections.emptySet())); foundBeacons.remove(beaconId); sharedPreferences.edit() .putStringSet(BEACON_LIST, storedBeacons) .putStringSet(FOUND_BEACON_LIST, foundBeacons) .apply(); HyperLog.d(TAG, "Remove scanning for beacon " + beaconId); Region region = new Region(beaconId, BeaconIdHelper.toAddress(beaconId)); if (regionBootstrap != null) { if (storedBeacons.isEmpty()) { HyperLog.i(TAG, "Disable scanning for beacons"); regionBootstrap.disable(); regionBootstrap = null; } else { regionBootstrap.removeRegion(region); } } }
Example #5
Source File: RegionBootstrap.java From android-beacon-library with Apache License 2.0 | 6 votes |
/** * Constructor to bootstrap your Application on an entry/exit from multiple regions * * @param application * @param regions */ public RegionBootstrap(BootstrapNotifier application, List<Region> regions) { if (application.getApplicationContext() == null) { throw new NullPointerException("The BootstrapNotifier instance is returning null from its getApplicationContext() method. Have you implemented this method?"); } this.context = application.getApplicationContext(); this.regions = regions; this.monitorNotifier = application; beaconManager = BeaconManager.getInstanceForApplication(context); beaconConsumer = new InternalBeaconConsumer(); if (beaconManager.isBackgroundModeUninitialized()) { beaconManager.setBackgroundMode(true); } beaconManager.bind(beaconConsumer); LogManager.d(TAG, "Waiting for BeaconService connection"); }
Example #6
Source File: AltBeaconScannerForegroundService.java From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void onCreate() { super.onCreate(); Log.d(TAG, "Starting foreground service"); beaconManager = BeaconManager.getInstanceForApplication(getApplicationContext()); Utils.setAltBeaconParsers(beaconManager); beaconManager.setBackgroundScanPeriod(5000); Foreground.init(getApplication()); Foreground.get().addListener(listener); ruuviRangeNotifier = new RuuviRangeNotifier(getApplicationContext(), "AltBeaconFGScannerService"); region = new Region("com.ruuvi.station.leRegion", null, null, null); startFG(); beaconManager.bind(this); medic = RuuviScannerApplication.setupMedic(getApplicationContext()); setBackground(); // start in background mode }
Example #7
Source File: RegionBootstrap.java From android-beacon-library with Apache License 2.0 | 6 votes |
/** * Constructor to bootstrap your Application on an entry/exit from a single region. * * @param context * @param monitorNotifier * @param region */ public RegionBootstrap(final Context context, final MonitorNotifier monitorNotifier, Region region) { if (context == null) { throw new NullPointerException("Application Context should not be null"); } this.context = context.getApplicationContext(); this.monitorNotifier = monitorNotifier; regions = new ArrayList<Region>(); regions.add(region); beaconManager = BeaconManager.getInstanceForApplication(context); beaconConsumer = new InternalBeaconConsumer(); if (beaconManager.isBackgroundModeUninitialized()) { beaconManager.setBackgroundMode(true); } beaconManager.bind(beaconConsumer); LogManager.d(TAG, "Waiting for BeaconService connection"); }
Example #8
Source File: MonitoringStatus.java From android-beacon-library with Apache License 2.0 | 6 votes |
public synchronized void updateNewlyInsideInRegionsContaining(Beacon beacon) { List<Region> matchingRegions = regionsMatchingTo(beacon); boolean needsMonitoringStateSaving = false; for(Region region : matchingRegions) { RegionMonitoringState state = getRegionsStateMap().get(region); if (state != null && state.markInside()) { needsMonitoringStateSaving = true; state.getCallback().call(mContext, "monitoringData", new MonitoringData(state.getInside(), region).toBundle()); } } if (needsMonitoringStateSaving) { saveMonitoringStatusIfOn(); } else { updateMonitoringStatusTime(System.currentTimeMillis()); } }
Example #9
Source File: RangingDataTest.java From android-beacon-library with Apache License 2.0 | 6 votes |
@Test // On MacBookPro 2.5 GHz Core I7, 10000 serialization/deserialiation cycles of RangingData took 22ms public void testSerializationBenchmark() 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(); for (int i=0; i < 10; i++) { beacons.add(beacon); } RangingData data = new RangingData(beacons, region); long time1 = System.currentTimeMillis(); for (int i=0; i< 10000; i++) { Bundle bundle = data.toBundle(); RangingData data2 = RangingData.fromBundle(bundle); } long time2 = System.currentTimeMillis(); System.out.println("*** Ranging Data Serialization benchmark: "+(time2-time1)); }
Example #10
Source File: BeaconLocatorApp.java From beaconloc with Apache License 2.0 | 6 votes |
@Override public void didExitRegion(Region region) { RegionName regName = RegionName.parseString(region.getUniqueId()); if (regName.isApplicationRegion()) { Log.d(Constants.TAG, "didExitRegion " + region); if (regName.getEventType() == ActionBeacon.EventType.EVENT_NEAR_YOU) { try { mBeaconManager.stopRangingBeaconsInRegion(region); // set "far" proximity mDataManager.updateBeaconDistance(regName.getBeaconId(), 99); } catch (RemoteException e) { Log.e(Constants.TAG, "Error stop ranging region: " + regName, e); } } if (regName.getEventType() == ActionBeacon.EventType.EVENT_LEAVES_REGION) { Intent intent = new Intent(); intent.setAction(Constants.NOTIFY_BEACON_LEAVES_REGION); intent.putExtra("REGION", (Parcelable) region); LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent); } } }
Example #11
Source File: BeaconLocatorApp.java From beaconloc with Apache License 2.0 | 6 votes |
@Override public void didEnterRegion(Region region) { RegionName regName = RegionName.parseString(region.getUniqueId()); if (regName.isApplicationRegion()) { Log.d(Constants.TAG, "didEnterRegion " + region); if (regName.getEventType() == ActionBeacon.EventType.EVENT_NEAR_YOU) { try { mBeaconManager.startRangingBeaconsInRegion(region); } catch (RemoteException e) { Log.e(Constants.TAG, "Error start ranging region: " + regName, e); } } if (regName.getEventType() == ActionBeacon.EventType.EVENT_ENTERS_REGION) { Intent intent = new Intent(); intent.setAction(Constants.NOTIFY_BEACON_ENTERS_REGION); intent.putExtra("REGION", (Parcelable)region); LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent); } } }
Example #12
Source File: BleManager.java From thunderboard-android with Apache License 2.0 | 6 votes |
@Override public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { Iterator<Beacon> iterator = beacons.iterator(); while (iterator.hasNext()) { //EditText editText = (EditText)RangingActivity.this.findViewById(R.id.rangingText); Beacon beacon = iterator.next(); Timber.d("beacon %s is about: %f meters away.", beacon.toString(), beacon.getDistance()); deviceFound(beacon); if (backgroundPowerSaver.isScannerActivityResumed()) { publishRecentBeacons(); } else if (SystemClock.elapsedRealtime() - backgroundPowerSaver.getScannerActivityDestroyedTimestamp() > ThunderBoardPowerSaver.DELAY_NOTIFICATIONS_TIME_THRESHOLD && backgroundPowerSaver.isApplicationBackgrounded()) { Timber.d("Sending notification."); sendNotification(beacon); } } }
Example #13
Source File: BeaconService.java From BeaconControl_Android_SDK with BSD 3-Clause "New" or "Revised" License | 6 votes |
@SuppressWarnings("unchecked") @Override public void handleMessage(Message msg) { Pair<Region, Long> obj = (Pair<Region, Long>) msg.obj; Region region = obj.first; long eventTimestamp = obj.second; if (msg.what == BeaconEvent.REGION_ENTER.ordinal()) { // if region has been marked to leave, we do not process enter event to have enter/leave events transparent for user if (!regionsToLeave.remove(region.getUniqueId())) { // region has not been marked to leave, process enter event processEvent(BeaconEvent.REGION_ENTER, eventTimestamp, region); } } else if (msg.what == BeaconEvent.REGION_LEAVE.ordinal()) { if (regionsToLeave.remove(region.getUniqueId())) { processEvent(BeaconEvent.REGION_LEAVE, eventTimestamp, region); } } else { ULog.d(TAG, "EnterLeaveDelayedHandler: unknown message."); } }
Example #14
Source File: ThunderBoardBootstrap.java From thunderboard-android with Apache License 2.0 | 6 votes |
/** * Used to disable additional bootstrap callbacks after the first is received. Unless this is called, * your application will be get additional calls as the supplied regions are entered or exited. */ public void disable() { Timber.d("Disabling region bootstrap"); if (disabled) { return; } disabled = true; try { for (Region region : regions) { beaconManager.stopMonitoringBeaconsInRegion(region); } } catch (RemoteException e) { Timber.d("Can't stop bootstrap regions"); } beaconManager.unbind(beaconConsumer); }
Example #15
Source File: ClientsManager.java From BeaconControl_Android_SDK with BSD 3-Clause "New" or "Revised" License | 6 votes |
private void removeMonitoredBeacons(String clientId) { Map<BeaconModel, BeaconEvent> newMonitoredBeacons = new HashMap<>(); for (Map.Entry<BeaconModel, BeaconEvent> entry : monitoredBeacons.entrySet()) { BeaconModel bm = entry.getKey(); if (bm.containsClient(clientId)) { bm.removeClient(clientId); removeTriggers(clientId, bm); } if (bm.hasClients()) { newMonitoredBeacons.put(bm, entry.getValue()); } else { try { beaconManager.stopMonitoringBeaconsInRegion(new Region(bm.getUniqueId(), null, null, null)); beaconManager.stopRangingBeaconsInRegion(new Region(bm.getUniqueId(), null, null, null)); } catch (RemoteException e) { ULog.d(TAG, "Cannot stop monitor/range region."); } } } monitoredBeacons = newMonitoredBeacons; }
Example #16
Source File: ScanHelper.java From android-beacon-library with Apache License 2.0 | 5 votes |
private List<Region> matchingRegions(Beacon beacon, Collection<Region> regions) { List<Region> matched = new ArrayList<>(); for (Region region : regions) { // Need to check if region is null in case it was removed from the collection by // another thread during iteration if (region != null) { if (region.matchesBeacon(beacon)) { matched.add(region); } else { LogManager.d(TAG, "This region (%s) does not match beacon: %s", region, beacon); } } } return matched; }
Example #17
Source File: BeaconService.java From BeaconControl_Android_SDK with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void processEvent(BeaconEvent beaconEvent, long eventTimestamp, Region region, double distance) { String beaconUniqueId = region.getUniqueId(); clientsManager.notifyClientsAboutBeaconProximityChange(beaconUniqueId, beaconEvent, eventTimestamp, distance); BeaconEvent oldBeaconEvent = clientsManager.getBeaconEvent(beaconUniqueId); if (beaconEvent == oldBeaconEvent) { ULog.d(TAG, getBeaconProximityId(region) + ", proximity is the same: " + beaconEvent.name()); return; } clientsManager.notifyClientsAboutAction(beaconUniqueId, beaconEvent, eventTimestamp); clientsManager.updateBeaconEvent(beaconUniqueId, beaconEvent); }
Example #18
Source File: RegionBootstrap.java From android-beacon-library with Apache License 2.0 | 5 votes |
/** * Add a new region * * @param region */ public void addRegion(Region region) { if (!regions.contains(region)) { if (serviceConnected) { try { beaconManager.startMonitoringBeaconsInRegion(region); } catch (RemoteException e) { LogManager.e(e, TAG, "Can't add bootstrap region"); } } else { LogManager.w(TAG, "Adding a region: service not yet Connected"); } regions.add(region); } }
Example #19
Source File: RegionBootstrap.java From android-beacon-library with Apache License 2.0 | 5 votes |
/** * Used to disable additional bootstrap callbacks after the first is received. Unless this is called, * your application will be get additional calls as the supplied regions are entered or exited. */ public void disable() { if (disabled) { return; } disabled = true; try { for (Region region : regions) { beaconManager.stopMonitoringBeaconsInRegion(region); } } catch (RemoteException e) { LogManager.e(e, TAG, "Can't stop bootstrap regions"); } beaconManager.unbind(beaconConsumer); }
Example #20
Source File: RegionBootstrap.java From android-beacon-library with Apache License 2.0 | 5 votes |
/** * Remove a given region * * @param region */ public void removeRegion(Region region) { if (regions.contains(region)) { if (serviceConnected) { try { beaconManager.stopMonitoringBeaconsInRegion(region); } catch (RemoteException e) { LogManager.e(e, TAG, "Can't stop bootstrap region"); } } else { LogManager.w(TAG, "Removing a region: service not yet Connected"); } regions.remove(region); } }
Example #21
Source File: MonitoringStatusTest.java From android-beacon-library with Apache License 2.0 | 5 votes |
@Test public void clearsStatusOfOver50RegionsTest() throws Exception { Context context = RuntimeEnvironment.application; MonitoringStatus monitoringStatus = new MonitoringStatus(context); for (int i = 0; i < 51; i++) { Region region = new Region(""+i, null, null, null); monitoringStatus.addRegion(region, null); } monitoringStatus.saveMonitoringStatusIfOn(); MonitoringStatus monitoringStatus2 = new MonitoringStatus(context); assertEquals("restored regions should be none", 0, monitoringStatus2.regions().size()); }
Example #22
Source File: BeaconLocatorApp.java From beaconloc with Apache License 2.0 | 5 votes |
public List<Region> getAllEnabledRegions() { List<Region> regions = new ArrayList<>(); List<ActionBeacon> actions = mDataManager.getAllEnabledBeaconActions(); for (ActionBeacon action : actions) { regions.add(ActionRegion.parseRegion(action)); } return regions; }
Example #23
Source File: MonitoringStatusTest.java From android-beacon-library with Apache License 2.0 | 5 votes |
@Test public void allowsAccessToRegionsAfterRestore() throws Exception { Context context = RuntimeEnvironment.application; BeaconManager beaconManager = BeaconManager.getInstanceForApplication(context); MonitoringStatus monitoringStatus = MonitoringStatus.getInstanceForApplication(context); for (int i = 0; i < 50; i++) { Region region = new Region(""+i, null, null, null); monitoringStatus.addRegion(region, null); } monitoringStatus.saveMonitoringStatusIfOn(); monitoringStatus.restoreMonitoringStatus(); Collection<Region> regions = beaconManager.getMonitoredRegions(); assertEquals("beaconManager should return restored regions", 50, regions.size()); }
Example #24
Source File: BeaconService.java From BeaconControl_Android_SDK with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void sendDelayedLeave(Region region) { Message msg = Message.obtain(); msg.what = BeaconEvent.REGION_LEAVE.ordinal(); msg.obj = new Pair<>(region, System.currentTimeMillis()); regionsToLeave.add(region.getUniqueId()); // mark that monitoring of region is intended to be stopped mEnterLeaveHandler.sendMessageDelayed(msg, config.getLeaveMsgDelayInMillis()); }
Example #25
Source File: ScanState.java From android-beacon-library with Apache License 2.0 | 5 votes |
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(); }
Example #26
Source File: RegionMonitorNotifier.java From PresencePublisher with MIT License | 5 votes |
@Override public void didEnterRegion(Region region) { String regionId = region.getUniqueId(); Set<String> foundRegions = new HashSet<>(sharedPreferences.getStringSet(FOUND_BEACON_LIST, Collections.emptySet())); if (!foundRegions.contains(regionId)) { HyperLog.i(TAG, "Found "); foundRegions.add(regionId); sharedPreferences.edit().putStringSet(FOUND_BEACON_LIST, foundRegions).apply(); } }
Example #27
Source File: BeaconService.java From android-beacon-library with Apache License 2.0 | 5 votes |
@MainThread public void stopRangingBeaconsInRegion(Region region) { int rangedRegionCount; synchronized (mScanHelper.getRangedRegionState()) { mScanHelper.getRangedRegionState().remove(region); rangedRegionCount = mScanHelper.getRangedRegionState().size(); LogManager.d(TAG, "Currently ranging %s regions.", mScanHelper.getRangedRegionState().size()); } if (rangedRegionCount == 0 && mScanHelper.getMonitoringStatus().regionsCount() == 0) { if (mScanHelper.getCycledScanner() != null) { mScanHelper.getCycledScanner().stop(); } } }
Example #28
Source File: BeaconService.java From android-beacon-library with Apache License 2.0 | 5 votes |
@MainThread public void startMonitoringBeaconsInRegion(Region region, Callback callback) { LogManager.d(TAG, "startMonitoring called"); mScanHelper.getMonitoringStatus().addRegion(region, callback); LogManager.d(TAG, "Currently monitoring %s regions.", mScanHelper.getMonitoringStatus().regionsCount()); if (mScanHelper.getCycledScanner() != null) { mScanHelper.getCycledScanner().start(); } }
Example #29
Source File: BeaconService.java From android-beacon-library with Apache License 2.0 | 5 votes |
@MainThread public void stopMonitoringBeaconsInRegion(Region region) { LogManager.d(TAG, "stopMonitoring called"); mScanHelper.getMonitoringStatus().removeRegion(region); LogManager.d(TAG, "Currently monitoring %s regions.", mScanHelper.getMonitoringStatus().regionsCount()); if (mScanHelper.getMonitoringStatus().regionsCount() == 0 && mScanHelper.getRangedRegionState().size() == 0) { if (mScanHelper.getCycledScanner() != null) { mScanHelper.getCycledScanner().stop(); } } }
Example #30
Source File: ActionRegion.java From beaconloc with Apache License 2.0 | 5 votes |
public static Region parseRegion(ActionBeacon actionBeacon) { if (actionBeacon == null) { throw new IllegalArgumentException("ActionBeacon object is null"); } String[] idents = actionBeacon.getBeaconId().split(";"); if (idents == null || idents.length < 3) { throw new IllegalArgumentException("ActionBeacon has invalid id"); } List<Identifier> identifiers = new ArrayList<>(); for (int i = 0; i < 3; i++) { identifiers.add(Identifier.parse(idents[i])); } return new Region(RegionName.buildRegionNameId(actionBeacon), identifiers); }