Run iPhone as an iBeacon in the background Run iPhone as an iBeacon in the background ios ios

Run iPhone as an iBeacon in the background


Standard CoreBluetooth advertisements can broadcast while the app is in the background, but not if they were started with CLBeaconRegion dictionary. The workaround is to ditch CoreLocation framework altogether and create your own proximity "framework" using only CoreBlueTooth.

You still need to use the appropriate background specifiers in the Info.plist file (e.g. bluetooth-peripheral and bluetooth-central).

The code looks something like this:

1) create a standard peripheral advertisement using CBPeripheralManager

NSDictionary *advertisingData = @{CBAdvertisementDataLocalNameKey:@"my-peripheral",                                  CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:identifier]]};// Start advertising over BLE[peripheralManager startAdvertising:advertisingData];

2) use use CBCentralManager to scan for that service using the UUID you specified.

NSDictionary *scanOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey:@(YES)};NSArray *services = @[[CBUUID UUIDWithString:identifier]];[centralManager scanForPeripheralsWithServices:services options:scanOptions];

3) in the CBCentralManagerDelegate method didDiscoverPeripheral, read the RSSI value of the advertisement.

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral     advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{    NSLog(@"RSSI: %d", [RSSI intValue]);}

4) Translate the RSSI values into a distance.

- (INDetectorRange)convertRSSItoINProximity:(NSInteger)proximity{    if (proximity < -70)        return INDetectorRangeFar;    if (proximity < -55)        return INDetectorRangeNear;    if (proximity < 0)        return INDetectorRangeImmediate;    return INDetectorRangeUnknown;}

I found that I needed to "ease" or "average" the RSSI values to get anything workable. This is no different than when you are working with any sensor data (e.g. accelerometer data).

I have this concept fully working hope to publish it somewhere at some point.

Also, use the docs (Core Bluetooth Programming Guide) if you get stuck.

Update: A full code sample is up on Github. I worked on this as part of a work related project.

Update #2: Apple release major improvements to iBeacon background behavior for iOS7.1


The Can you smell the iBeacon? article discusses both the use of Estimotes and advertising from Macs and iOS devices. You need to check the capability “Acts as Bluetooth LE accessory” in the project target.


No, iOS devices only advertise iBeacon when the app that does the advertising runs in the foreground. so, if you switch to another app or if the device goes to sleep, the advertisement stops.

Of course, if you really want the advertisement to continue, disable the idle timer and do Guided Access so that the iOs device does not go to sleep and no one can switch to another app.