'iOS Extending background time for beacon monitoring

I'm developing an iOS swift app for traveling, the concept of the app is after validating the trip with the user, while in trip, the app keeps ranging for bluetooth beacons, I'm using this lib to do this https://github.com/AltBeacon/ios-beacon-tools

As i saw this lib uses CoreBluetooth and it works fine in background. The problem is, since doing a trip, the trip can even last 1 to 2 hours long, and I need to scan beacons (for example every 15 seconds) during that period to register the route the user has taken.

Right now is whenever I enter in the background with my app I only have 30 seconds for beacon ranging and then the iOS kills my app (maybe because I don't have an expiration handler that's what i read on forums) but still I need to keep the beacon ranging going for an indefinite period of time that can last from 10 minutes to 2 hours.

How am I able to achieve this ? I'm not that experienced in iOS so please help me.



Solution 1:[1]

What you want to do is possible.. But it is tricky.

A few points:

  1. In the background, you cannot ever detect AltBeacon or other manufacturer advertisements With CoreBluetooth as iOS blocks manufacturer advertisements from being delivered in the background. (iBeacon advertisements follow different rules in the background when you use CoreLocation.)

  2. In the background, service advertisements like Eddystone can be detected.

  3. Generally your app can only run for a few seconds after being sent to the background, then detections will stop. However, you can extend this time by up to 180 seconds upon request using the technique described here. While my blog post is about CoreLocation, the same technique works for CoreBluetooth.

  4. As the above article mentions, it is also possible to extend the 180 seconds to forever by doing the following:

  • Set the BackgroundModes in your Info.plist to include location.
  • Obtain location always permission in addition to Bluetooth permission.
  • Use CoreLocation to start major location updates. (You do not have to use the location results that come from this, but it is needed to keep the app alive in the background.)
  • Start a background task as described in the blog post linked above.

This will keep your app scanning for Bluetooth service beacons like Eddystone in the background forever, but there are three catches:

  1. If you want to get your app approved for the App Store you must convince Apple reviewers that your app provides the user a location-specific benefit that is obvious to the user.
  2. iOS will present your users a scary dialog every 3 days or so warning them the app is using your location in the background and showing a map with all the places the user was when the app was running. The dialog will allow the user to disable background location permission, and if the user does this, background beacon updates will stop. See my blog post here for more info.
  3. There will be significant battery drain while your app is doing constant Bluetooth scanning.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1