/** Notifies the listener about possible region-based events */ private void notifyListener() { IBeacon newNearestBeacon = null; if (_arrOrderedIBeacons.size() > 0) newNearestBeacon = _arrOrderedIBeacons.get(0); // Case 1: enter iBeacon region from nowhere if (_previousNearestIBeacon == null && newNearestBeacon != null) { _listener.enterRegion(newNearestBeacon); _previousNearestIBeacon = newNearestBeacon; } // Case 2: keep in the same iBeacon region, update proximity else if (_previousNearestIBeacon != null && newNearestBeacon != null && _previousNearestIBeacon.equals(newNearestBeacon)) { _previousNearestIBeacon = newNearestBeacon; } // Case 3: enter a different iBeacon region (roaming) else if (_previousNearestIBeacon != null && newNearestBeacon != null && !_previousNearestIBeacon.equals(newNearestBeacon)) { _listener.exitRegion(_previousNearestIBeacon); _listener.enterRegion(newNearestBeacon); _previousNearestIBeacon = newNearestBeacon; } // Case 4: leave iBeacon region else if (_previousNearestIBeacon != null && newNearestBeacon == null) { _listener.exitRegion(_previousNearestIBeacon); _previousNearestIBeacon = null; } }
/** * Finds an ibeacon in the list of previously discovered ibeacons * * @param b ibeacon to find * @return <code>null</code> if the ibeacon is not found, the existing copy of the ibeacon * otherwise. */ private IBeacon findIfExists(IBeacon b) { for (int i = 0; i < _arrOrderedIBeacons.size(); i++) { IBeacon existing = (IBeacon) _arrOrderedIBeacons.get(i); if (existing.equals(b)) return existing; } return null; }