gpt4 book ai didi

ios - CBPeripheral track写入记录

转载 作者:行者123 更新时间:2023-11-28 21:33:50 24 4
gpt4 key购买 nike

我正在从数据库中检索数据记录,然后将每条记录写入 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

}

最佳答案

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);
}
}

关于ios - CBPeripheral track写入记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34716641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com