Receive Signal from Beacon While App Is in the Background

Receive signal from beacon while app is in the background

You can only range beacons (which is what gives you access to the distance approximations) when the app is running. As soon you leave the app (press the home button, navigate to another app, etc.), iOS will suspend the app, and ranging will stop working. Continuous usage of Bluetooth radio in the background would drain the smartphone's battery quickly.

You can, however, have your app subscribe to be woken up into the background when the smartphone enters and exits range of a beacon (or a group of beacons). This is called region monitoring, and it's the same mechanism that geofencing uses.

Now, "enter" and "exit" events in and on themselves won't give you access to distance approximations. However, since iOS will wake your app into the background for a few seconds to handle these events, ranging will actually resume for the duration (assuming you haven't stopped it before the app got suspended), before iOS puts the app back to sleep again.

You can even extend the "few seconds" into up to a few minutes with a background execution task.

All of the above doesn't require the use of background modes—only the "always" authorization to use Location Services.

You can't ordinarily keep the app running in the background indefinitely with beacons. Background support is heavily regulated by Apple, and is only allowed, e.g., for navigation apps, or music apps. People do on occasion try using the "location" background mode to keep the app alive in the background (and thus capable of ranging beacons), and some even reported being able to get it past the review process, but that seems to be more of an exception than a rule.

Should you decide to give it a try anyway, you need to:

  • enable the "location" background mode,
  • set allowsBackgroundLocationUpdates to true on your CLLocationManager instance,
  • start regular location updates: startUpdatingLocation.

This should keep the app running in the background even if you leave it.

is it possible to run Scan Beacon when app is Killed

Yes, it is absolutely possible to resume beacon scanning on both Android and iOS after an app is killed. But the specifics of how you do this are different between iOS and Android. Further, the use of non-native development frameworks like FlutterBeacon make things more complicated as the documentation is not as complete for this use case.

While I am not an expert on FlutterBeacon, I am the lead developer on the underlying Android Beacon Library which it uses under the hood. I am also very familiar with how iOS CoreLocation works (which it also uses under the hood.)

In general here are the rules to get apps to detect after being killed:

iOS:

  • Start beacon monitoring (not just ranging) in the didFinishLaunching method of the AppDelegate
  • Obtain ALWAYS Location permission
  • After the app is killed it will be re-launched in the background when all beacons disappear or the first beacon appears in a monitored region.

Android:

  • Start beacon monitoring in the onCreate method of an Android application class using RegionBootstrap. See here for a discussion on the FlutterBeacon github
  • Obtain BACKGROUND_LOCATION permission from the user.
  • On Android 8+, your app should restart within 25 minutes of being killed and give you a callback to the BootstrapNotifier methods as appropriate.

auto launch application beacon every time when detect beacon signal

In general this is possible, yes.

The RegionBootstrap class is designed to auto launch your app into the background to scan for beacons, and send callbacks whenever you enter/exit a beacon region. Because the operating system may kill your app to save memory, the library is designed to re-launch the app so its background scanning service can continue operating under a few conditions:

  1. Within 5 minutes of being killed using an Android AlarmManager.
  2. If the above fails, on a power connect/disconnect operation.
  3. On phone boot.

Following the reference you mention, this behavior should be automatic.

While you can't define a radius of a Region, you can add filtering logic that does something similar. Once you get a callback to didEnterRegion, start ranging for beacons in that region. In the ranging callback, if you see that beacon.getDistance() < 5.0 you can execute your logic that you want to happen only if the beacon is less than five meters away.

Android background BLE beacon scanning

Two points:

  1. All bluetooth beacon formats use the same radio transport mechanism. Radio power is limited by international regulatory bodies making transmitters weak.

    The laws of physics decide how well they will travel between floors of a building, which will vary based on construction materials, but results will generally be poor. Again, this will be the same for all beacon formats.

  2. Android 8+ limits detections of BLE in the background without a foreground service. You can do at most one scan every 10-25 minutes (this is randomized) when the phone is not in deep sleep mode. the only alternative is a foreground service with an ever present notification.

I realize these are not the answers you want to hear, but it is important to recognize and accept real-world limitations that exist and come up with creative solutions that work within these limitations.

Can we start an iBeacon transmitter in the background?

Unfortunately, no. iOS does allow background advertising of Bluetooth Services, but only using a proprietary technique that breaks the iBeacon advertising format. As a result, if your app starts transmitting as an iBeacon, then switches to the background, its transmission will still exist, but it will no longer be picked up by iBeacon detectors. See here for more information:

...you should be aware that advertising while your app is in the background operates differently than when your app is in the foreground. In particular, when your app is advertising while in the background ...[a]ll service UUIDs contained in the value of the CBAdvertisementDataServiceUUIDsKey advertisement key are placed in a special “overflow” area; they can be discovered only by an iOS device that is explicitly scanning for them.

The bottom line is that iOS devices cannot transmit as iBeacons when they are in the background.

Edit 4/11/2020: Apple announced plans for a new beacon advertisement that will work in the background. See here for more info.

IBeacon background monitoring

Apple restricts the duration in which iOS apps may "range" beacons in the background. This is true as of April 2020, and the state of affairs is largely unchanged in the last 5-6 years.

A few clarifications:

  1. Beacon monitoring is not restricted in the background, provided the user grants "location always" permission to the app. But beacon monitoring only gives you "entry" and "exit" events. It gives you an entry event when any beacon you are looking for first appears, and an exit event when the last of the beacons you are looking for disappears. Monitoring won't tell you anything about the estimated distance or signal strength of an individual beacon.

  2. Beacon ranging lets you get updates every second with a list of all beacons visible, each beacon's signal strength and a distance estimate. You can easily program logic to write to a database if the distance estimate is under as certain threshold.

  3. Beacon ranging is restricted in the background on iOS. First, the user must grant "location always" access to your app. And even then you are limited to about 5-10 seconds of ranging after your app goes to a background, or beacons first appear after it is in the background.

  4. You can extend the 5-10 seconds mentioned in (3) to 180 seconds simply by running a background task per my blog post here.

  5. You can further extend the 180 seconds indefinitely if you declare "location" background mode in your Info.plist, use a background task as described in (4), and you use CoreLocation to request location updates (coarse location updates with only a 3km resolution work fine for this purpose and save battery by keeping the GPS off.)

  6. One problem with (5) is that it may get your app rejected if you plan to submit it to the App Store and you don't provide an obvious user-facing benefit to using location in the background. If you do have a good justification, or you intend to distribute the app outside the App Store (e.g. a corporate enterprise app), then this will work fine.

  7. A second problem with (5) is that ranging all the time can cause significant battery drain. You may need to program extra logic to stop this constant background ranging at times where it is not needed in order to save battery.

This is admittedly confusing and complex. But the bottom line is that there are ways to range beacons indefinitely to satisfy many use cases. You just have to jump through some hoops to make it happen.



Related Topics



Leave a reply



Submit