I am working on an Application with the Android Beacon Library with Android 8+ and I need to do ranging in the background. For this purpose I use the nativly supported possibility to start the service as a foreground service. The Application should keep ranging, even when the application is closed! So implementing the BeaconConsumer Interface in an activity is not a good idea, beacuse if the Acitivity gets removed from the memory ranging will obviously stop. To work around this I created a custom application class and implemented a BeaconConsumer and started ranging in this component. So the custom application is the Consumer and handles the results in the onServiceConnect(...) Method. Do I need the BootstrapNotifier for my goal, or will it work like this? Will the Application keep ranging if the application is closed?
public class App extends Application implements BeaconConsumer {
onCreate(){
mbeaconManager =
org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
mbeaconManager.setDebug(true);
// Create Notification for the Foreground Service
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
);
Notification notification = new
NotificationCompat.Builder(getApplicationContext(),CHANNEL_1_ID)
.setSmallIcon(R.drawable.icon)
.setContentTitle("\"Ranging runs in background!")
.setContentIntent(pendingIntent)
.build();
// Set Scanning Settings
mbeaconManager.enableForegroundServiceScanning(notification,
notificationID);
mbeaconManager.setEnableScheduledScanJobs(false);
mbeaconManager.setBackgroundBetweenScanPeriod(100);
mbeaconManager.setBackgroundScanPeriod(100);
}
public void onBeaconServiceConnect() {
mbeaconManager.removeAllRangeNotifiers();
mbeaconManager.addRangeNotifier(new RangeNotifier() {
public void didRangeBeaconsInRegion(...) {
// handle found beacons
}
mbeaconManager.startRangingBeaconsInRegion(...);
}
}
}