首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >核心蓝牙ios 8

核心蓝牙ios 8
EN

Stack Overflow用户
提问于 2014-09-30 12:26:36
回答 1查看 1.1K关注 0票数 0

我使用核心蓝牙连接两个ios设备。对于ios 7来说,一切都是完美的,但对于ios 8来说,没有。带有ios 8的设备作为外围设备不可见。

一些代码:

代码语言:javascript
运行
复制
    _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];
    }


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-03 12:58:33

iOS 8.0 bluetooth peripheral manager giving no callback for addService -这个问题帮助我解决了这个问题。只是需要移动一下

代码语言:javascript
运行
复制
 [_peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey: @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]}];

-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral方法:

代码语言:javascript
运行
复制
-(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来说,一切都很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26120834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档