【问题标题】:CBPeripheral track written recordCBPPeripheral 跟踪记录
【发布时间】:2016-01-11 08:03:15
【问题描述】:

我正在从数据库中检索数据记录,然后将每条记录写入 BLE 设备的CBCharacteristic。编写实现,

for service in self.connectedPeripheral!.services! {

   for characteristic in service.characteristics! {

       if characteristic.UUID.UUIDString == "1111" {

         self.connectedPeripheral!.writeValue(dataRecord, forCharacteristic: characteristic, type: .WithResponse)

       }
   }
}

现在CBPeripheraldidWriteValueForCharacteristic 方法被nil NSError 调用。所以写成功了。但是我们如何跟踪写入 BLE 设备的数据记录。因为我需要从数据库中删除这条成功写入的记录。在CBCharacteristic 文档中,当我们开始对BLE 进行写入操作时,我找不到任何可以设置为数据库record_id 的属性。其中大部分是只读的。

func peripheral(peripheral: CBPeripheral, didWriteValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {

   print("write characteristic: \(characteristic)")
   // Need to track the record here So it can be removed from database

}

【问题讨论】:

    标签: ios core-bluetooth cbperipheral


    【解决方案1】:

    peripheral:didWriteValueForCharacteristic:error方法得到响应后,必须调用该方法[peripheral readValueForCharacteristic:characteristic]

    这样你会在peripheral:didUpdateValueForCharacteristic:error:中得到回调 在这里您可以检查您的特征值,如果记录成功写入您的特征,则删除该记录。

    -(void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
    
      if (error == nil) {
       NSString *characteristicValue=[[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
       NSLog(@"value=%@",characteristicValue);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      • 1970-01-01
      • 2011-04-06
      • 2021-05-23
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多