iBeacon开始在应用程序上做广告确实进入后台

时间:2014-05-14 12:54:41

标签: ios cllocationmanager core-bluetooth cbperipheral

applicationDidEnterBackgroundapplicationWillResignActive后我需要startAdvertising,但我收到此错误:

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

我用:

- (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");
    }
}

任何帮助将不胜感激..

2 个答案:

答案 0 :(得分:2)

如果您想主动从您的应用中发送 BT广告数据,我认为您的应用必须在前台。 这来自Apple Class Reference

  

[..]数据广告是在“尽力而为”的基础上完成的,因为空间有限,并且可能会有多个应用同时广告。 当您的应用位于前台时,对于支持的广告数据键的任意组合,它可以在初始广告数据中使用最多28个字节的空间[..]

在后台,你只能收听信标。为此,您必须将您的应用和信标数据注册到CLLocationManager

答案 1 :(得分:1)

要消除此错误,请等待在委托方法peripheralManagerDidUpdateState中调用CBPeripheralManager方法startAdvertising:。这里的关键是在执行任何CBPeripheralManager方法之前确保外围状态始终等于CBPeripheralManageStatePoweredOn。