2013-04-16 48 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) { 
    ... 
}