【问题标题】:iBeacon start advertising on application did enter backgroundiBeacon 在应用程序上开始广告确实进入了后台
【发布时间】:2014-05-14 12:54:41
【问题描述】:

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

任何帮助将不胜感激..

【问题讨论】:

    标签: ios cllocationmanager core-bluetooth cbperipheral


    【解决方案1】:

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

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

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

    【讨论】:

    • 你能想出一种方法来“绕过”这个“限制”并且仍然获得应用商店的批准吗?
    • 根据我的阅读,Apple 限制 CoreLocation 框架在后台做广告。不过好像可以用CoreBluetooth在后台发送BT广告数据。我不知道您是否可以通过这种方法获得完整的 iBeacon 功能,我也不知道 Apple 是否会在 AppStore 中批准它......但值得一试。这是一个处理这个问题的 GitHub 项目github.com/Instrument/Vicinity
    【解决方案2】:

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

    【讨论】:

    • 虽然这是前台状态的正确答案,但 iOS 11 在 background 中的行为是:即使 peripheral.state 是 .poweredOn 广告也无法启动并且记录错误“API MISUSE: 只能在开机状态下接受此命令”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多