核心蓝牙内部的可写特性

时间:2013-04-16 10:34:06

标签: iphone ios objective-c core-bluetooth

我正在使用核心蓝牙。我已经添加了写入/读取特征的功能。在此之前,我想检查特征是否可写。我为此目的使用了characteristic.properties,我的代码是:

    if (characteristic.properties==CBCharacteristicPropertyRead)
    {
        [peripheralDevice readValueForCharacteristic:characteristic];
    }
    else if(characteristic.properties==CBCharacteristicPropertyWrite)
    {
        [peripheralDevice setNotifyValue:YES forCharacteristic:characteristic];
    }
  NSLog(@"the property :%d",currentCharacteristic.properties );

这里是文档中的Characteristics.properties的枚举:

typedef NS_ENUM(NSInteger, CBCharacteristicProperties) {
    CBCharacteristicPropertyBroadcast                                               = 0x01,
    CBCharacteristicPropertyRead                                                    = 0x02,
    CBCharacteristicPropertyWriteWithoutResponse                                    = 0x04,
    CBCharacteristicPropertyWrite                                                   = 0x08,
    CBCharacteristicPropertyNotify                                                  = 0x10,
    CBCharacteristicPropertyIndicate                                                = 0x20,
    CBCharacteristicPropertyAuthenticatedSignedWrites                               = 0x40,
    CBCharacteristicPropertyExtendedProperties                                      = 0x80,
    CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0)     = 0x100,
    CBCharacteristicPropertyIndicateEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0)   = 0x200
};

它的工作正常,从特征中读取价值。但问题是在写入值时,它不会进入第二个循环。我已经设置了可写属性的特性。对于我检查过的两个外围设备,我得到136的打印声明。请提出一些解决方案以解决这个问题?

2 个答案:

答案 0 :(得分:2)

使用ANDing操作解决了这个问题:

 if (characteristic.properties&CBCharacteristicPropertyRead!=0)
 {
      [peripheralDevice readValueForCharacteristic:characteristic];
 }

答案 1 :(得分:0)

这是Swift 3的解决方案:

if characteristic.properties.contains(.write) {
    ...
}