iOS 8.0蓝牙外设管理器不为addService提供回调

Nik*_*ant 3 core-bluetooth ios8

我创建了一个蓝牙外设管理器.我正在使用[self.peripheralManager addService:myService]向该外设管理器添加一些服务.

对于iOS7.0我得到了这个方法的回调 - (void)peripheralManager:(CBPeripheralManager*)外设didAddService:(CBService*)服务错误:(NSError*)错误

但对于iOS8.0,我没有获得相同方法的任何回调,我无法建立连接.

以下是我遵循的步骤:

  1. 使用[[CBPeripheralManager alloc] initWithDelegate:self queue:nil options创建外设管理器:@ {CBPeripheralManagerOptionShowPowerAlertKey:@YES}];

2.使用myService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@"AAAA"] primary:YES]创建服务;

  1. 使用myCharacteristic = [[CBMutableCharacteristic alloc] initWithType创建一个特性:[CBUUID UUIDWithString:@"BBBB"]属性:CBCharacteristicPropertyIndi​​cate value:nil permissions:CBAttributePermissionsReadable];

  2. 使用myService.characteristics = characteristic将此特性添加到服务中

  3. 使用[peripheralManager addService:myService]将此服务添加到外围设备管理器;

  4. 上述步骤的回调发生在

    • (void)peripheralManager:(CBPeripheralManager*)外设didAddService :( CBService*)服务错误:(NSError*)错误;
  5. 在iOS8中,我们没有收到此回调.

外围设备管理器的更新状态代码如下所示.

-(void) peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
    NSLog(@"Changed State : %d", peripheral.state);
    if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
        [self deviceDisconnected];
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用以下方法添加服务:

[self.peripheralManager addService:myService];

这种方法在iOS8.0中是否已弃用,或者此错误可能是由于某些其他原因造成的?

Nik*_*ant 8

@Paulw在评论中正确回答了这个问题.以下是答案

在蓝牙硬件初始化(开机状态)之前ios7允许的操作之间的ios7和ios8之间有变化并发出警告.在ios8中它只是失败了.

所以我们必须peripheralManagerDidUpdateState:在添加任何服务之前等待回调.