snksnk
snksnk

Reputation: 1595

iBeacon start advertising on application did enter background

Upon applicationDidEnterBackground or applicationWillResignActive I need to startAdvertising but I get this error:

CoreBluetooth[API MISUSE] <CBPeripheralManager: 0x146a4e30> can only accept this command while in the powered on state.

I use:

- (void)applicationWillResignActive:(UIApplication *)application
{
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    [_locationManager stopRangingBeaconsInRegion:_runningBeacon];
    NSLog(@"stop monitoring");
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"23542266-18D1-4FE4-B4A1-23F8195B9D39"];
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:1 minor:1 identifier:@"com.devfright.myRegion"];
    self.beaconPeripheralData = [self.beaconRegion peripheralDataWithMeasuredPower:nil];
    self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
    [self.peripheralManager startAdvertising:self.beaconPeripheralData];

    if ([self.peripheralManager isAdvertising]) {
        NSLog(@"peripeheralManager is advertising");
    }
}

Any help would be appreciated..

Upvotes: 0

Views: 1357

Answers (2)

Justin Moser
Justin Moser

Reputation: 2005

To silence this error, wait to call the CBPeripheralManager method startAdvertising: in the delegate method peripheralManagerDidUpdateState:. The key here is to make sure the peripheral state is always equal to CBPeripheralManageStatePoweredOn before performing any CBPeripheralManager methods.

Upvotes: 1

Argent
Argent

Reputation: 390

If you want to actively send out BT advertisement data from your app, I think your app has to be in the foreground. This is from the Apple Class Reference

[..]Data advertising is done on a “best effort” basis, because space is limited and there may be multiple apps advertising simultaneously. While your app is in the foreground, it can use up to 28 bytes of space in the initial advertisement data for any combination of the supported advertising data keys[..]

While in background, you can only listen to beacons. For that you have to register your app and beacon data to the CLLocationManager

Upvotes: 2

Related Questions