CoreBluetooth:无法编写和检索静态特性

时间:2015-01-20 00:05:05

标签: ios7 bluetooth-lowenergy core-bluetooth

我正在构建一个应用程序,其中两个iOS设备相互传输和扫描(外围设备和中央设备)。由于Apple的实施,当应用程序背景化时,所有可识别信息都将从广告数据包中删除,这意味着我需要连接到已发现的外围设备,以了解它们在后台传输时的身份和内容。

我真正需要做的就是识别外围设备。 (连接和断开)。目前,我能找到的唯一方法是设置附加到公共服务的静态特性,允许每个设备唯一地标识自己,即使在后台运行时也是如此。此值不会更改或更新。如果我可以在连接后简单地查看peripheral.UUID,这就可以了。但我不能再使用iOS8了。因此,我创建了一个包含唯一标识符的特性。

(不确定这是否是最佳方式,但这是我能想到的唯一方法。)

一切都很好(发现特征),但我无法检索除了nil之外的任何特性,即使我在开始传输时已经专门设置了它。

这是我的(外围代码):

-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {

    // Opt out from any other state
    if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
        return;
    }
    NSLog(@"BT Transmitter Powered On");

    NSString* uniqueString = @“foobar";
    NSData* characteristicValue = [uniqueString dataUsingEncoding:NSUTF8StringEncoding];

    self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"08590F7E-DB05-467E-8757-72F6FAEB13D4"]
                    properties:CBCharacteristicPropertyRead
                    value:characteristicValue
                    permissions:CBAttributePermissionsReadable];

    CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@"E20A39F4-73F5-4BC4-A12F-17D1AD07A961"] primary:YES];

    transferService.characteristics = @[self.transferCharacteristic];       
    [self.peripheralManager addService:transferService];

    [self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey: @[[CBUUID UUIDWithString:@"E20A39F4-73F5-4BC4-A12F-17D1AD07A961"]] }];
}

这是我的中央密码:

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    if (error) {
        NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
        return;
    }
    for (CBCharacteristic *characteristic in service.characteristics) {
        // print out value of discovered characteristic
        NSLog (@"Characteristic discovered: %@", characteristic);  // this outputs all all the properties of the characteristic, including a value of "null".
        NSString *value = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
        NSLog(@"Value: %@",value); // this prints out nothing
    }
}

我做错了什么?我希望看到特征的价值为" foobar"当转换回NSString时。相反,它是null。

2 个答案:

答案 0 :(得分:2)

发现了执行读取请求以实际获取其值所需的特性 -

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    if (error) {
        NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
        return;
    }
    for (CBCharacteristic *characteristic in service.characteristics) {
        // print out value of discovered characteristic
        NSLog (@"Characteristic discovered: %@", characteristic);  // this outputs all all the properties of the characteristic, including a value of "null".
        if ([characteristic.UUID.UUIDString isEqualToString:@"08590F7E-DB05-467E-8757-72F6FAEB13D4"]) {
            [peripheral readValueForCharacteristic:characteristic];
        NSString *value = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
        NSLog(@"Value: %@",value); // this prints out nothing
    }
}

随后您将致电didUpdateValueForCharacteristic: -

-(void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {

    if (error == nil) {
       NSString *valueString=[[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
       NSLog(@"The value=%@",valueString
    }
}

答案 1 :(得分:0)

更新:一旦连接到外围设备并读取其服务,找到外围设备的RSSI,请使用readRSSI方法。

然后,奇怪的是,即使它不在文档中,这是唯一的委托回调方法(使用RSSI),对我来说运行8.1.1。

- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error {     NSLog(@"在didReadRSSI中获得RSSI更新:%4.1f",[RSSI doubleValue]); }

现在,我只需要弄清楚如何将此RSSI信号与之前呼叫中连接并识别的特定外设相关联。