ios - 写 CBCharacteristic 的问题

标签 ios objective-c bluetooth-lowenergy core-bluetooth ibeacon

我目前正在开发扫描我公司信标的 BLE 应用程序,我需要更新 CBCharacteristic 的值。

我在写入函数后没有收到任何错误,但更改并未反射(reflect)在 BLE 设备上。

我已经扫描了所有 CBCharacteristics,然后遍历得到特定的并调用写入函数

for (CBCharacteristic* characteristic in beaconCharacteristics)
{
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF5"]]) {
        [characteristic.service.peripheral writeValue:[@"BeaconE" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
    }
}

即使我在写入后没有错误地调用了完成委托(delegate)方法

-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
}

我错过了什么吗?是因为十六进制值吗?

请指导我

谢谢!

最佳答案

Characteristic 可能是只读的。您可以通过查看 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

}

关于ios - 写 CBCharacteristic 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29970085/

相关文章:

ios - 使用键更新字典值

ios - 从隐藏状态栏的 View Controller 进行交互式 View Controller 转换

ios - 权利与配置文件不匹配 (0xE8008016)

objective-c - NSTreeController add 和 addChild 方法

ios - 如何使用 Depth Api 去除肖像背景?

iphone - 以编程方式将函数锚定到 UIButton

ios - 从外设启动 CoreBluetooth 重新连接

android - BLE扫描记录说明

android - Android中BluetoothLeScanner的setReportDelay的目的是什么

iOS 可达性不起作用