I am using Android Beacon Library for BLE scanning with example. It works fine in foreground for both monitoring and ranging. However, for background, it only works for the cases of pressing "Home" in app and screen off. It is not work when I kill the app from task switcher. In the example, I cannot find anything like Service to make things working in background.
Questions:
- Does Android Beacon Library support background scanning if the app is killed in task switcher?
- If so, how to do this? Any example?
No, nor does any other app. Killing a task via Task Switcher means killing the process of that app, and since your background scan relies on a service, that service is killed too since services don't start their own process.
You can probably have an AlarmManager that periodically checks if your service is running and starts it automatically if it's not, but I'd imagine that would hit the battery big time if you set the interval too low.
I worked with android iBeaon library in that for background scanning I created a service and in service I defined both monitoring and ranging. I start the service when application is distroy and its work for me. Create new service like this.
In AndroidManifest.xml
Yes, the Android Beacon Library will continue detecting beacons after the app is killed, but it can take time for scanning to resume. See here for details: http://altbeacon.github.io/android-beacon-library/resume-after-terminate.html
The library uses an AlarmManager to accomplish this, and the scanning can take up to five minutes to resume.
The reference app you link to actually has an example of how to set this up using the
RegionBootstrap
class. A more descriptive example is in the code examples under the Starting an App in the Background section here: http://altbeacon.github.io/android-beacon-library/samples.html