ios – CBPeripheralManager未启动

我一直收到一个错误,说我的CBPeripheralManager没有开机,但在我的代码中,我觉得我实现了这一点.这是我的代码
- (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按钮做广告:

- (IBAction)advertise:(id)sender {
    [self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] }];
    [self.peripheralManager startAdvertising:@{ CBAdvertisementDataTxPowerLevelKey : @(YES)}];
}

解决方法

您需要将所有调用都包含在状态检查中的CBPeripheralManager中,以防止出现这些警告.由于您刚刚在不确定的时间调用广告,因此您需要确保您的外围设备管理器仍然处于供电状态并准备就绪.
- (IBAction)advertise:(id)sender
{
  if(self.peripheralManager.state == CBPeripheralManagerStatePoweredOn)
  {
    //Now you can call advertise
  }
}

相关文章

背景 前端时间产品经理决定使用百度统计,使得 工程B 中原统计sdk-友盟统计,需要被去除。之前尝试去除...
结论: alloc负责分配内存和创建对象对应的isa指针; init只是返回alloc生成的对象。 所以alloc后,多次...
更新 如果UI愿意把启动图切割成n份,按一定约束在launchscreen.storyboard中进行排版,启动图效果会更好...
最近在看一本书《Effective OC 2.0》,今天看到有个tip是OC适中循环各自优劣性,作者最终推荐此块循环。...
// // ViewController.m // paintCodeTestOC //gif // Created by LongMa on 2019/7/25. // #import &a...
背景介绍 一般情况下,出于省电、权限、合理性等因素考虑,给人的感觉是很多奇怪的需求安卓可以实现,但...