How Monitoring in the background works on Android?

Follow

Region Monitoring is a basic way for apps to interact with beacon regions. It allows for detecting when a mobile device enters or exits the range of beacons in a particular region. Regions are defined by the same values as beacons: UUID, Major, and Minor.

To learn more about Monitoring, read the following articles:

Monitoring in the background on Android

Apps can use startMonitoring method of BeaconManager class to start Monitoring for regions. Monitoring updates arrive at a mobile device registered with the setMonitoringListener method ofBeaconsManager class.

Monitoring is designed to perform periodic scans in the background. By default it scans for 10 seconds and sleeps for 30 seconds. Therefore, it can take up to 40 seconds to detect an enter/exit event in the region. You can change the scanning frequency via BeaconManagers setBackgroundScanPeriod.

Keep in mind that if the app is killed by device memory requirements, it is possible to carry out background monitoring to relaunch it. However, if the user killes the app background monitoring won't work.

Estimote Android SDK is publicly available on GitHub.

Was this article helpful?
3 out of 3 found this helpful

24 Comment(s)

  • Avatar
    Jorge Oliva Fernández

    Hi,

    Can this Background Monitoring auto launches my app on iBeacon detection??
    or is only for notifications??

    Best Regards.

  • Avatar
    Ula Kierwiak

    Hi Jorge,

    No, Background Monitoring won't auto launch your app. It's impossible in Android devices for now. But if you have your app launched already and you are in the range of an Estimote Beacon, you will get notifications even if you don't actively use the app.

    Please note iBeacon is an Apple invention and as such is working with Apple devices only, but Estimote beacons broadcast two protocols.

  • Avatar
    Nikolas LeBlanc

    Is it still the case that this remains impossible? I've seen plenty of documentation indicating otherwise, but loose ends such as these confuse the matter greatly. Can we get an update on this post and others like it to direct people to new documentation when and if it exists?

    Thanks.

  • Avatar
    Wojtek Borowicz

    Hi Nikolas,

    Yes, monitoring in the background works on Android. But, unlike on iOS, it will not work if the app has been shut down completely (removed from the app switcher).

    You can find more details in our Java Docs: http://estimote.github.io/Android-SDK/JavaDocs/

    Cheers.

  • Avatar
    Nikolas LeBlanc

    Hey Wojtek, thanks for the reply.

    Perhaps I'm not understanding something, but I'm pretty sure that I've gotten Android monitoring in the background for my beacons with the app completely shut down. Maybe 'completely shut down' means something different to Android and iOS?

    Granted, the app has to have been opened at least once, but from that point on, after reboots and ever when removed from the app switcher, I get notifications when entering and exiting regions.

  • Avatar
    Piotr Krawiec

    Interesting! Anything you had to do to achieve that? I just tried with my own Android app and I couldn't get notifications to work after I removed it from the app switcher.

  • Avatar
    Nikolas LeBlanc

    Hey Piotr, check out the following repo (https://github.com/knowledgetranslation/BeaconPlaygroundAndroid/), where I've uploaded my working folder. Try it out and please let me know how it works for you. I'd be as interested to hear that it helped you out as I would be learn that I'm missing something.

  • Avatar
    Allan Barthelot

    Hey Piotr,
    I managed to implement background scanning by triggering a service thats scans for beacons for 10 seconds every 1 or 2 minutes to run through the AlarmManager. Because of the interval between, it doesnt seem to affect battery life.

  • Avatar
    Matthew Stenback

    Any follow up to clarify this? I noticed the article was update in December.

  • Avatar
    Nikolas LeBlanc

    Yeah, was hoping to hear back from Piotr too. Haven't touched it since I first posted here, but my repo still hosts a working copy of beacon scanning as a background service.

  • Avatar
    Piotr Krawiec

    Hey Nikolas—I'm really sorry, this have completely escaped my mind. I'm travelling till the end of month and don't have access of my Android test devices, so I can't try this out in practice. I'm putting this on my todo list for when I'm back in the office, let's touch base then.

  • Avatar
    Nikolas LeBlanc

    Sure thing Piotr. No worries.

  • Avatar
    Federico Alvarez

    Nikolas, Piotr, been reading this post and did not manage to get it to work. i bought myself 3 estimotes and got it to work with the app running in background, but not as a service.

    I tried Nikolas code, but did not work. The service gets registered and stays up and running, but nevet gets to see my beacons.

    What's your experience? Anyone else got to try this? Any suggestion?

    Best regards,
    Federico.

  • Avatar
    Federico Alvarez

    (all this in an Android 4.3 device)

  • Avatar
    Nikolas LeBlanc

    Hey Federico, that's.... weird. I'd have expected it to fail setting up the service, not finding the Beacons. Have you had any success finding your beacons with other sample code - say simpler "Hello World" equivalents?

    Also, is there any indication when you run my code that the scan is ongoing? Have you tried walking around while it's active? Have you ensured that you're using the correct beacon ids?

    Let me know.

  • Avatar
    Nikolas LeBlanc

    Hey Federico, I just tested the repo again myself - so I cloned the repo, opened the folder in Android Studio, opened the Project View pane (option+1), found MyActivity and ran it with my device connected.

    What I found was that it didn't immediately start the scan, but when I turned Bluetooth off and then on, everything fired up and started working properly.

    Try that out and see if you have any more luck with it.

  • Avatar
    Federico Alvarez

    Thanks Nikolas for the prompt reply.

    The repo I used is: https://github.com/knowledgetranslation/BeaconPlaygroundAndroid/

    What I'm trying to achieve is to open an app, start a background service, then close the app, and let the service monitor for beacons in range.

    In your app that would be done by entering "notify demo", selecting a beacon, and then closing the app. If I do so, the "onDestroy" method is called and it calls "beaconsManager.disconnect()", so no further notice will be received. I commented that line (also the "notificationManager.cancel(NOTIFICATION_ID);" so notifications still work).

    So, with those lines commented I open your app, go to "notify", select a beacon, then press "back" twice to be in the "AllDemosActivity" and walk away. No notification of the "Exited region" is performed.

    This only work if, when in the "notify" activity, I press the "home" button, in that case the notification of entering o exiting happens.

    Is it possible to run this monitoring code a background service?

    Maybe even run it when phones boot, with no user interaction. I'm thinking of something that extends "Service" and is called with "AlarmManager" every one minute or so.

    (I'm not interested in whether if this consumes to much battery, or if users should choose when apps run and such, this is a proof of concept I'm working on)

    Thanks a lot Nikolas.

    Best regards,
    Federico.

  • Avatar
    Federico Alvarez

    I just saw your first questions, sorry:

    • Have you had any success finding your beacons with other sample code - say simpler "Hello World" equivalents?

      • Yes, with your code, Estimotes demo APK, and some code of my one I get to see the beacons just fine. It is getting difficult to find them in full background.
    • Also, is there any indication when you run my code that the scan is ongoing?

      • Not really sure how to check this, the listener is encapsulated in estimotes JAR I believe. All I call is "beaconManager.startMonitoring(region);" and then get an answer in "public void onEnteredRegion(Region region, List<Beacon> beacons) {". Is there a way to check this? Because I'm pretty sure that the monitor dies.
    • Have you tried walking around while it's active?

      • Yes, with the app active it works just fine. Even if I press the "home" or "power" button. It stops working if I press the "back" button instead.
    • Have you ensured that you're using the correct beacon ids?

      • Yes, al IDs, Majors and Minors are working fine. Even though the region is defined as "region = new Region("regionId", __UUID, null, null);", so every beacon in that UUID is seen.

    Thanks again.

    Best regards,
    Federico.

  • Avatar
    Nikolas LeBlanc

    Not sure where "notify demo" is or what it refers to. I think you may be getting my app mixed up with the Estimote app? I'll recap the point of my app, just to be clear about its intention.

    What my repo represents is a proof of concept. It shows that you can make an app that can register background scanning of beacons a) with the app fully closed, and b) upon reboot, without needing to open the app first.

    I've just updated the repo to start scanning upon app launch.

    This is what the app does:

    1) When you launch it, it will start looking for Beacons. It will keep looking for beacons when in the background, and when closed from the application switcher.

    2) When you toggle the Bluetooth state of your phone, it will toggle whether or not the service is running.

    3) When you reboot your phone, if Bluetooth is set to activate by default, when it's state is toggled it will toggle the service as above.

    The app itself does nothing, it's just a white screen. It's just to show that the service can be setup in the background.

    I've tried this with the repo directly and it worked as expected - I've even tried using the back button to see if I could break it to no avail. Can you be clearer about this app's role in what you're describing? Can you confirm whether or not this app is behaving as I've described on your end?

    I'm testing this with a Samsung Galaxy S4. I haven't tested this with any other device.

  • Avatar
    Federico Alvarez

    Thanks Nikolas. Let me give this a try tomorrow morning. Will let you know of the outcome.

    Best regards,
    Federico.

  • Avatar
    Barrie Vince

    I think there was an important part that was mentioned in passing and might have been missed: "Granted, the app has to have been opened at least once...".

    See the second answer in this post http://stackoverflow.com/questions/5051687/broadcastreceiver-not-receiving-boot-completed

  • Avatar
    Nikolas LeBlanc

    Right. This is absolutely true. Sorry if this wasn't clear.

  • Avatar
    Simon Stettler

    @NikolasLeBlanc
    Exactly what I was looking for. Awsome, thank you so much!

  • Avatar
    Nikolas LeBlanc

    Great! Glad it worked out for you Simon.

Estimote is
hiring!