Run iPhone as an Ibeacon in the Background

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.

How to run iPhone as a beacon in background in both xcode(swift) and phonegap

Unfortunately, Apple blocks the ability to transmit as a beacon in the background on iOS. All CoreBluetooth standard advertisements are actually blocked, because advertising slots are a limited resource and multiple apps in the background would exhaust this resource quickly.

Apple does have a proprietary backup system for advertising Bluetooth services in the background that uses a special overflow area to include the service identifiers. But while this scheme works for advertising Bluetooth services in the background, it does not work for advertising beacons, because it breaks the format.

Bottom line: background beacon advertising on iOS just does not work. Sorry.

Can iOS app act as beacon, even when it is running in the background?

Unfortunately, iOS apps cannot emit iBeacon advertisements unless they are in the foreground. If you program your app to advertise iBeacon frames, as soon as the screen turns off or the app is not visible, iOS changes the advertising format to a special generic background advertisement that does not match the iBeacon format.

This proprietary background advertisement is useless for identification of a device. It is designed for connection to BLE GATT services from other iOS devices. Unless you want to advertise a connectable BLE GATT service to other iOS devices this advertisement has little value. EDIT: There is a way to make use of this advertisement. See below.

Apple's documentation describes this limitation here in the section titled "The bluetooth-peripheral Background Execution Mode":

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:

The CBAdvertisementDataLocalNameKey advertisement key is ignored, and the local name of peripheral is not advertised.
All 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.
If all apps that are advertising are in the background, the frequency at which your peripheral device sends advertising packets may decrease.

It may not be obvious that the above causes iBeacon transmissions to break, but I assure you that is absolutely a consequence.

Read my blog post here for more information on how to put useful beacon data into this background "Overflow Area" advertisement.

iOS advertising beacon in background

I wrote my master thesis about this topic. The answer is no, it is not possible to make an iOS App advertise beacon data in background.

It doesn't work with the traditional CoreLocation APIs. What you may be able to do, is to replicate the iBeacon behavior in your app while supporting background broadcast and detection, but I did not manage to make the broadcasted signal to be recognised as a beacon.

Check this answer for additional information:
https://stackoverflow.com/a/19941436/3726570

Monitoring iBeacon beacons in background

An iOS device has a limited number of hardware acceleration slots for beacon detection. These slots defer to the bluetooth chip to notify the operating system when a beacon region pattern has been matched. When these slots are used, background detections are very quick -- a region entry event can happen within a second or two of the beacon transmission being in range.

If these slots run out, however, then the operating system must fall back to full scans to find the remaining regions. The operating system throttles these to save battery. Evidence suggests that they take place about every 15 minutes. This means that if any regions monitored by your app that don't get a hardware acceleration slot can take up to 15 minutes to be detected after a matching beacon comes into range (although on average it will often take less time -- 7.5 minutes would be the average)

How many hardware slots are available? This is undocumented and may vary by iOS device model. But experimentation shows it may be about 30. This is across all apps on the phone and is completely unrelated to the 20 region limit per app. The first apps to register regions are the first apps to get the slots.

Read more here: https://developer.radiusnetworks.com/2015/04/21/max-beacon-regions-ios.html

It is possible that the above is what you are experiencing, and there are only 3 hardware acceleration slots left on your phone for your app. To test this theory, you can:

  1. Uninstall other apps that you expect may be registering beacon regions, which should free up more slots.
  2. Wait up to 15 minutes to see if you get detections for the other regions in the background.

how to advertise in the background as a beacon when entering a beacon region

Sorry, iOS will not let your app advertise a BLE packet matching the iBeacon spec when it is in the background. As you have mentioned in your question, Apple alters the way background advertisements emitted in the background look, and as a result, they will not trigger CoreLocation enter events to wake up receiving apps in the background.

There are a few imperfect alternatives:

  1. You can use other beacon advertisements in the background and wake up your app. This won't be as quick as iBeacon, but it will wake up your app within a few minutes. Here is a setup that does that: https://github.com/Decemberlabs/AltBeacon

  2. You can advertise iBeacon whenever the app is in the foreground, and try to get the user to bring the app to the foreground through a local notification.

  3. If there are other foreground copies of your app in the vicinity (based on background beacon detection), you can use a web service to notify them of your presence during the 10 seconds your app runs in the background. This notification can tell them to start advertising on your behalf.

How to use ibeacon when application in background and iphone reboot

Nothing special is needed to detect beacons in the background after reboot using CoreLocation monitoring APIs. If detections work without a reboot they should work with one.

The important thing to know about the bootup process on iOS is that it can take several minutes after reboot before CoreLocation is fully initialized and responding to Bluetooth LE beacons. When testing after a reboot, be extra patient. The fact that illuminating the display accelerates this process does not change that this is true.



Related Topics



Leave a reply



Submit