I am trying to notify the user when the user enters a region defined and also when the user exits the region. This is what i have done so far. I used to use the rangeNotifier method (didBeaconsEnterRange) in order to notify a user when he/she enters a region, but then this method would get called every 1 second, so i moved the notification logic to the monitorNotifier class (didEnterRegion) method.
My application class extends BootStrapNotifier, and i am setting the scanPeriod to 2000s instead of the default 1100l seconds, because i seem to receive exit and entry notifications way too quick, even when the beacon is in range. Earlier i even increased the timeout period from 10000ms to 20000ms that would fire the exit when a beacon doesnt fire signals within the timeout period.
Code snippet for myapplication class
BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(LAYOUT_HERE));
beaconManager.setForegroundScanPeriod(2000l);
beaconManager.setBackgroundBetweenScanPeriod(1100l);
//beaconManager.setBackgroundScanPeriod(5000l);
beaconManager.setDebug(true);
Log.d(TAG, "setting up background monitoring for beacons and power saving");
// wake up the app when a beacon is seen
mRegion = new Region("myRangingUniqueId",
null, null, null);
setRegionAgain();
SetRegionAgain method
if(SharedPrefs.getString(SharedPrefs.iBEACON_ID, "").trim().length() > 0) {
if(SharedPrefs.getString(SharedPrefs.iBEACON_ID, "").trim().length() > 0 ) {
try {
mRegion = new Region("myRangingUniqueId",
Identifier.parse(SharedPrefs.getString(SharedPrefs.iBEACON_ID, "")),
null, null);
}catch(IllegalArgumentException e) {
e.printStackTrace();
mRegion = new Region("myRangingUniqueId",
null, null, null);
}
}
}
regionBootstrap = new RegionBootstrap(this, mRegion);
// simply constructing this class and holding a reference to it in your custom Application
// class will automatically cause the BeaconLibrary to save battery whenever the application
// is not visible. This reduces bluetooth power usage by about 60%
backgroundPowerSaver = new BackgroundPowerSaver(this);
I have a background service that does the notification work, so it implements the BeaconConsumer interface. Code snippet below:
OnStart method:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
flagToCheckExit = false;
mHandler = new Handler();
beaconManager.bind(this);
if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(false);
return Service.START_STICKY;
}
onDestroy method:
@Override
public void onDestroy() {
super.onDestroy();
try {
beaconManager.stopMonitoringBeaconsInRegion(CommonUtilities.getRegion());
// this region is the one that i use to monitor (singleton types)
} catch (RemoteException e) {
e.printStackTrace();
}
beaconManager.unbind(this);
}
onServiceConnected method:
@Override
public void onBeaconServiceConnect() {
beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didExitRegion(final Region region) {
LogManager.d(TAG, "didExitRegion %s", region);
if(SharedPrefs.getString(SharedPrefs.USER_ID, "").trim().length() > 0) {
flagToCheckExit = true;
// i use this flag to prevent random entry and exit notifications
mHandler.postDelayed(mRunnable, 20000);
// waiting for 20seconds before firing an exit notification, since an entry notification might get fired immediately after the exit
}
}
@Override
public void didEnterRegion(Region region) {
Log.e(TAG,"region id1 >>> " + ((region.getId1() == null) ? "null" : region.getId1().toUuidString()));
LogManager.d(TAG, "didEnterRegion %s ",region);
if(!flagToCheckExit) {
if(SharedPrefs.getString(SharedPrefs.USER_ID, "").trim().length() > 0) {
if(region.getId1() != null &&
region.getId1().toUuidString().equalsIgnoreCase(SharedPrefs.getString(SharedPrefs.iBEACON_ID, ""))) {
if(!SharedPrefs.getBoolean(SharedPrefs.IS_ENTRY_LOG_CALLED, false)) {
String entryRange = getAppContext().getString(R.string.entered_beacon_region);
CommonUtilities.sendNotification(MonitoringAltBeaconService.this,entryRange,1);
}
}
}
}else {
// invalidate the handler
// stop all operations of the handler
// we do this to prevent an exit getting called since entry has been called immediately.
mHandler.removeCallbacks(mRunnable);
}
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
LogManager.d(TAG, "didDetermineStateForRegion %s ",region);
}
});
startMonitoring();
}
startMonitoring method:
private void startMonitoring() {
try {
if(SharedPrefs.getString(SharedPrefs.iBEACON_ID, "").trim().length() > 0 ) {
try {
beaconManager.startMonitoringBeaconsInRegion(CommonUtilities.getRegion());
}catch(IllegalArgumentException e) {
e.printStackTrace();
beaconManager.startMonitoringBeaconsInRegion(CommonUtilities.getRegion());
}
}
} catch (RemoteException e) { }
}
the runnable thread:
Runnable mRunnable = new Runnable() {
@Override
public void run() {
SharedPrefs.putBoolean(SharedPrefs.IS_ENTRY_LOG_CALLED, false);
SharedPrefs.putBoolean(SharedPrefs.IS_EXIT_LOG_CALLED, true);
String exitedRange = getAppContext().getString(R.string.exited_beacon_region);
CommonUtilities.sendNotification(MonitoringAltBeaconService.this,exitedRange,2);
LogManager.d(TAG, "exit called");
flagToCheckExit = false;
}
};
There is a strange behavior with this, there are multiple entry and exit logs that i get even when the beacon device is in range, i get the exit. I tried bypassing the exit notification but it seems to fail with the logic (patch) above.
Log:
03-19 18:00:25.866: D/MonitoringAltBeaconService(22795): didDetermineStateForRegion id1: cbb7c628-a321-4cf6-934d-37dbfa335735 id2: null id3: null
03-19 18:00:25.867: D/MonitoringAltBeaconService(22795): didExitRegion id1: cbb7c628-a321-4cf6-934d-37dbfa335735 id2: null id3: null
03-19 18:00:26.470: D/MonitoringAltBeaconService(22795): didDetermineStateForRegion id1: cbb7c628-a321-4cf6-934d-37dbfa335735 id2: null id3: null
03-19 18:00:26.477: D/MonitoringAltBeaconService(22795): didEnterRegion id1: cbb7c628-a321-4cf6-934d-37dbfa335735 id2: null id3: null
03-19 18:00:48.076: D/MonitoringAltBeaconService(22795): didDetermineStateForRegion id1: cbb7c628-a321-4cf6-934d-37dbfa335735 id2: null id3: null
03-19 18:00:48.076: D/MonitoringAltBeaconService(22795): didExitRegion id1: cbb7c628-a321-4cf6-934d-37dbfa335735 id2: null id3: null
03-19 18:00:51.275: D/MonitoringAltBeaconService(22795): didDetermineStateForRegion id1: cbb7c628-a321-4cf6-934d-37dbfa335735 id2: null id3: null
03-19 18:00:51.282: D/MonitoringAltBeaconService(22795): didEnterRegion id1: cbb7c628-a321-4cf6-934d-37dbfa335735 id2: null id3: null
03-19 18:01:10.269: D/MonitoringAltBeaconService(22795): didDetermineStateForRegion id1: cbb7c628-a321-4cf6-934d-37dbfa335735 id2: null id3: null
03-19 18:01:10.269: D/MonitoringAltBeaconService(22795): didExitRegion id1: cbb7c628-a321-4cf6-934d-37dbfa335735 id2: null id3: null
03-19 18:01:15.876: D/MonitoringAltBeaconService(22795): didDetermineStateForRegion id1: cbb7c628-a321-4cf6-934d-37dbfa335735 id2: null id3: null
03-19 18:01:15.883: D/MonitoringAltBeaconService(22795): didEnterRegion id1: cbb7c628-a321-4cf6-934d-37dbfa335735 id2: null id3: null
I am using AprilBeacon. The app i am working on is held up because the beacons dont generate notifications correctly. Please assist.
EDIT:
the device i use if Motorolla g2, this scenario happens in both the kitkat as well as lollipop versions. And Wifi is on, i prefer it being on, since there are webservice calls involved, when an entry exit is determined. Realtime , it would not be encouraged to ask users to switch their wifi off :(..
The library i use is , android-beacon-library the one here, and i am afraid i do not know the frequency at which the april beacon sends out signals.
EDIT 2:
Log 1 can be found here
Log 2 can be found here
EDIT 3
log when wifi is switched off I noticed that i did get an exit even when the beacon was in range And i opened the locate app, and it showed no beacons are there (see screenshot). i removed the battery and put them back it got the beacon, and so did my app too. (but in real life, i am sure batteries will not be tampered with)