我目前正在开发一个扫描我的公司信标的BLE应用程序,我需要更新CBC特性的值。
写入函数后我没有收到任何错误,但更改并未反映在BLE设备上。
我已经扫描了所有CBC特性,然后遍历以获取特定的并调用写入函数
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
{
}
我错过了什么吗?是由于十六进制值吗?
请指导我
谢谢!
答案 0 :(得分:10)
可能是特征是只读的。您可以通过查看CBCharacteristicProperties对象找到,您可以使用characteristic.properties
//Check if the Characteristic is writable
if ((characteristic.properties & CBCharacteristicPropertyWrite) ||
(characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse))
{
//Do your Write here
}
下面是一个简单的方法,它接受CBCharacteristicProperties对象并记录属性。 。 .helps调试。
-(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
}
答案 1 :(得分:0)
Swift 3版
if((characteristic.properties.contains(CBCharacteristicProperties.write)) || (characteristic.properties.contains(CBCharacteristicProperties.writeWithoutResponse)))
{
//Do your Write here
}