核心蓝牙iOS 8 [英] Core bluetooth ios 8
本文介绍了核心蓝牙iOS 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Core Bluetooth连接两个ios设备。一切对于ios 7来说都是完美的,但是对于ios 8则没有。装有ios 8的设备作为外围设备不可见。
I use Core Bluetooth to connect two ios devices. Everything is working perfect for ios 7, but for ios 8 no. Device with ios 8 is not visible as peripheral device.
某些代码:
_peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
[_peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey: @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]}];
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
if (error) {
[self cleanup];
return;
}
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]] forService:service];
}
}
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
if(error){
[self cleanup];
return;
}
for (CBCharacteristic *charactristic in service.characteristics) {
if ([charactristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
[peripheral setNotifyValue:YES forCharacteristic:charactristic];
}
}
}
- (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error) {
return;
}
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
[peripheral setNotifyValue:NO forCharacteristic:characteristic];
[_centralManager cancelPeripheralConnection:peripheral];
[_data appendData:characteristic.value];
NSDictionary *recievedData = [NSDictionary dictionaryWithObject:stringFromData forKey:@"receivedData"];
[[NSNotificationCenter defaultCenter]
postNotificationName:DATA_FROM_PERIPHERAL_RECEIVED
object:self userInfo:recievedData];
// }
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
return;
}
if (characteristic.isNotifying) {
NSLog(@"Notification began on %@", characteristic);
}else{
[_centralManager cancelPeripheralConnection:peripheral];
}
}
推荐答案
iOS 8.0蓝牙外设管理器不提供addService的回调-这个问题可以帮助我解决此问题。只需移动
iOS 8.0 bluetooth peripheral manager giving no callback for addService - This question helps me to fix this issue. Just need to move
[_peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey: @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]}];
到-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
方法:
-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
return;
}
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
[_peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey: @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]}];
self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];
CBMutableService *transferService = [[CBMutableService alloc]initWithType:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID] primary:YES];
transferService.characteristics = @[_transferCharacteristic];
[_peripheralManager addService:transferService];
}
}
现在所有东西都对ios 6很有用,7,8。
Now everything works great for ios 6,7,8.
这篇关于核心蓝牙iOS 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文