书面问题 CBCharacteristic
Posted
技术标签:
【中文标题】书面问题 CBCharacteristic【英文标题】:Issue in writing CBCharacteristic 【发布时间】:2015-04-30 14:12:31 【问题描述】:我目前正在开发一个扫描我公司信标的 BLE 应用程序,我需要更新 CBCharacteristic 的值。
在我的写入函数之后我没有收到任何错误,但更改不会反映在 BLE 设备上。
我已经扫描了所有的 CBCharacteristics,然后遍历获取特定并调用 write 函数
for (CBCharacteristic* characteristic in beaconCharacteristics)
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF5"]])
[characteristic.service.peripheral writeValue:[@"BeaconE" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
即使我在写入后调用完成委托方法也没有错误
-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
我错过了什么吗?是因为十六进制值吗?
请指导我
谢谢!
【问题讨论】:
写入新数据后,peripheral:didWriteValueForCharacteristic:error:
如果您尝试读取它,值是否已更改?它到底有什么变化?它的名字?如果是,您尝试检查设备名称是否更改,可能是由于某些缓存。
我有 name 、 minor 、 major 、 udid 等的 CBCharacteristics 。peripheral 中的值也不会改变:didWriteValueForCharacteristic:error:
写入新值后,您需要对特征发出读取操作,然后才能在 CoreBluetooth 中看到新值。如果设备中的值实际上没有改变,那么要么是您的设备有问题,要么您没有发送正确的序列。
【参考方案1】:
特征可能是只读的。您可以通过查看 CBCharacteristicProperties 对象来了解,您可以使用characteristic.properties 来了解
//Check if the Characteristic is writable
if ((characteristic.properties & CBCharacteristicPropertyWrite) ||
(characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse))
//Do your Write here
下面是一个简单的方法,它接受一个 CBCharacteristicProperties 对象并记录属性。 . .帮助调试。
-(void)logCharacteristicProperties:(CBCharacteristicProperties)properties
if (properties & CBCharacteristicPropertyBroadcast)
NSLog(@"CBCharacteristicPropertyBroadcast");
if (properties & CBCharacteristicPropertyRead)
NSLog(@"CBCharacteristicPropertyRead");
if (properties & CBCharacteristicPropertyWriteWithoutResponse)
NSLog(@"CBCharacteristicPropertyWriteWithoutResponse");
if (properties & CBCharacteristicPropertyWrite)
NSLog(@"CBCharacteristicPropertyWrite");
if (properties & CBCharacteristicPropertyNotify)
NSLog(@"CBCharacteristicPropertyNotify");
if (properties & CBCharacteristicPropertyIndicate)
NSLog(@"CBCharacteristicPropertyIndicate");
if (properties & CBCharacteristicPropertyAuthenticatedSignedWrites)
NSLog(@"CBCharacteristicPropertyAuthenticatedSignedWrites");
if (properties & CBCharacteristicPropertyExtendedProperties)
NSLog(@"CBCharacteristicPropertyExtendedProperties");
if (properties & CBCharacteristicPropertyNotifyEncryptionRequired)
NSLog(@"CBCharacteristicPropertyNotifyEncryptionRequired");
if (properties & CBCharacteristicPropertyIndicateEncryptionRequired)
NSLog(@"CBCharacteristicPropertyIndicateEncryptionRequired");
此外,您应该检查错误以查看 CoreBluetooth 是否引发错误。
//CBPeripheral Delegate
-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
if (error)
NSLog(@"<error> didWriteValueForCharacteristic %@",[error description]);
//
// Add Error handling for failed Writes
//
//Add Handling for successful writes
【讨论】:
我实际上必须纠正其他一些 CBCharacteristic 才能获得可读形式的 CBCharacteristic。【参考方案2】:Swift 3 版本
if((characteristic.properties.contains(CBCharacteristicProperties.write)) || (characteristic.properties.contains(CBCharacteristicProperties.writeWithoutResponse)))
//Do your Write here
【讨论】:
以上是关于书面问题 CBCharacteristic的主要内容,如果未能解决你的问题,请参考以下文章