撰写CBC特色的问题

Que*_*kir 2 objective-c ios core-bluetooth bluetooth-lowenergy ibeacon

我目前正在研究一个扫描我的公司信标的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];
    }
}
Run Code Online (Sandbox Code Playgroud)

甚至我在写完后没有错误就得到了完成委托方法

-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?是否由于十六进制值?

请指导我

谢谢 !

小智 10

可能是特征是只读的.您可以通过查看CBCharacteristicProperties对象找到它,您可以使用characteristic.properties获取该对象

//Check if the Characteristic is writable
if ((characteristic.properties & CBCharacteristicPropertyWrite) ||
    (characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse))
{
    //Do your Write here
}
Run Code Online (Sandbox Code Playgroud)

下面是一个简单的方法,它采用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");
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,您应该检查错误以查看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

}
Run Code Online (Sandbox Code Playgroud)