首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CBPeripheralManager没有开机

CBPeripheralManager没有开机
EN

Stack Overflow用户
提问于 2013-12-16 00:59:34
回答 1查看 4.5K关注 0票数 6

我一直收到一个错误,说我的CBPeripheralManager没有打开,但是在我的代码中,我觉得我实现了这一点。这是我的代码:

代码语言:javascript
复制
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Start up the CBPeripheralManager
    _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
    // Start up the CBCentralManager

    // And somewhere to store the incoming data
    _data = [[NSMutableData alloc] init];
}

/** Required protocol method.  A full app should take care of all the possible states,
 *  but we're just waiting for  to know when the CBPeripheralManager is ready
 */
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {

    if (peripheral.state == CBPeripheralManagerStatePoweredOn) {

        // We're in CBPeripheralManagerStatePoweredOn state...
        NSLog(@"self.peripheralManager powered on.");

        // ... so build our service.

        // Then the service
        CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID] primary:YES];

        // And add it to the peripheral manager
        [self.peripheralManager addService:transferService];
    }
}

然后,我打电话给我的外围设备,用IBAction按钮开始做广告:

代码语言:javascript
复制
- (IBAction)advertise:(id)sender {
    [self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] }];
    [self.peripheralManager startAdvertising:@{ CBAdvertisementDataTxPowerLevelKey : @(YES)}];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-16 02:03:16

为了防止这些警告,您需要将对CBPeripheralManager的所有调用都包装在状态检查中。因为你只是在一个未定的时间打电话给广告,你需要确保你的peripheralManager仍然有动力,并准备好去做。

代码语言:javascript
复制
- (IBAction)advertise:(id)sender
{
  if(self.peripheralManager.state == CBPeripheralManagerStatePoweredOn)
  {
    //Now you can call advertise
  }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20601927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档