public class EstimoteMonitoringManager {
private static final int NOTIFICATION_ID = 123;
private static BeaconManager beaconManager;
private static NotificationManager notificationManager;
private static final UUID uuid = UUID.fromString("sdfsdfsdf-sdfsdf-sdfsdf");
private static final int MAJOR_ID = 23624;
private static final int MINOR_ID = 18570;
private static Context context;
private Region range = new Region();
public static void Create(NotificationManager notificationMngr,
Context context, final Intent i) {
try {
notificationManager = notificationMngr;
EstimoteMonitoringManager.context = context;
beaconManager = new BeaconManager(EstimoteMonitoringManager.context);
beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1),
0);
beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> beacons) {
postNotificationIntent("Estimote testing",
"I have found an estimote !!!", i);
}
@Override
public void onExitedRegion(Region region) {
postNotificationIntent("Estimote testing",
"I have lost my estimote !!!", i);
}
});
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
Timber.d("BeaconManger Connected!");
for (int i = 0; i < 19; i++) {
beaconManager.startMonitoring(new Region("region1", uuid, MAJOR_ID + i, MINOR_ID + i));
}
}
});
} catch (Exception e) {
Crashlytics.logException(e);
}
}
// Pops a notification in the task bar
public static void postNotificationIntent(String title, String msg, Intent i) {
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivities(
context, 0, new Intent[] { i },
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(context)
.setSmallIcon(android.R.drawable.ic_dialog_info).setContentTitle(title)
.setContentText(msg).setAutoCancel(true)
.setContentIntent(pendingIntent).build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(NOTIFICATION_ID, notification);
}
// Stop beacons monitoring, and closes the service
public static void stop() {
try {
beaconManager.stopMonitoring();
beaconManager.disconnect();
} catch (Exception e) {
Crashlytics.logException(e);
}
}
}
In the above snippet i am trying to create 20 regions for monitoring using a loop(not sure whether its efficient).However i want to stop monitoring those regions after a particluar interval.Created a Stop() method which stops monitoring,issue is how can i stop those regions which i have created inside a loop??