iOS 8.0 bluetooth peripheral manager giving no callback for addService

耗尽温柔 提交于 2020-01-11 09:51:14

问题


I have created a bluetooth peripheral manager. I am adding some services to this peripheral manager using [self.peripheralManager addService:myService].

For iOS7.0 I am getting a callback for this method - (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error

But for iOS8.0 I am not getting any callback for the same method and I am not able to establish a connection.

Here are the steps that I followed:

  1. Create a peripheral manager using [[CBPeripheralManager alloc] initWithDelegate: self queue:nil options:@{CBPeripheralManagerOptionShowPowerAlertKey: @YES}];

2.Create a service using myService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@"AAAA"] primary:YES];

  1. Create a characteristic using myCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"BBBB"] properties:CBCharacteristicPropertyIndicate value:nil permissions:CBAttributePermissionsReadable];

  2. Add this characteristic to the service using myService.characteristics = characteristic

  3. Add this service to the peripheral manager using [peripheralManager addService:myService];

  4. The callback for this above step occurs in

    • (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error ;
  5. In iOS8 we are not receiving this callback.

The code for peripheral manager did update state is shown below.

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

I am adding the service using:

[self.peripheralManager addService:myService];

Is this method deprecated in iOS8.0 or this error might be due to some other reasons?


回答1:


@Paulw has answered this correctly in the comments. Below is the answer

There is change between ios7 and ios8 in that ios7 permitted operations before the Bluetooth hardware was initialised (power on state) and issued a warning. In ios8 it just fails.

So we must wait for the peripheralManagerDidUpdateState: callback before adding any service.



来源:https://stackoverflow.com/questions/26008296/ios-8-0-bluetooth-peripheral-manager-giving-no-callback-for-addservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!