问题
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:
- 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];
Create a characteristic using myCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"BBBB"] properties:CBCharacteristicPropertyIndicate value:nil permissions:CBAttributePermissionsReadable];
Add this characteristic to the service using myService.characteristics = characteristic
Add this service to the peripheral manager using [peripheralManager addService:myService];
The callback for this above step occurs in
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error ;
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