0

applicationDidEnterBackground或者applicationWillResignActive我需要,但我收到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");
    }
}

任何帮助,将不胜感激..

4

2 回答 2

2

如果你想从你的应用主动发送BT广告数据,我认为你的应用必须在前台。这是来自 Apple 类参考

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

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

于 2014-05-14T13:41:14.497 回答
1

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.

于 2014-05-20T23:37:25.437 回答