2013-04-16 46 views
1

我正在使用核心蓝牙。我添加了写入/读取特征的功能。在此之前,我想检查特性是否可写。我已经使用了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); 

这里是从文档的Characteristic.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。请提出一些解决方案来解决这个问题?

+0

我不明白,而是:你的房子可读写?因为,在你的'if ... ELSE if'中,你将在第一个循环中输入,但不在第二个循环中输入... – Larme

+0

你从NSLog语句中得到了什么?获取136看起来很奇怪,因为这个枚举的最大值应该是9.如果你一直得到136,那么读取部分也不起作用。 – sarfata

回答

1

通过取与操作解决了这个:

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

这是斯威夫特3解决方案:

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