1
votes

I am working with the core bluetooth . I have added the functionality to write/read on the characteristics . Before that I want to check whether the characteristic is writable or not . I have used the characteristic.properties for this purpose my code is :

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

here is the enum for the Characteristic.properties from the documentation :

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
};

Its working fine for the reading the value from the characteristic. But problem is while writing the value it is not going inside the second loop. I have set the characteristic with the writable property . I am getting the 136 in print statement for both of the peripheral on which I have checked . Please suggest some solution to overcome this problem ?

2
I didn't understand all, but: is your property writable AND readable ? Because, of your if...ELSE if, you'll enter in the first loop, but not in the second one... - Larme
What do you get out of that NSLog statement? Getting 136 seems really weird, because the max value for this enum should be 9. Also if you get 136 all the time, then the read part would also not work. - sarfata

2 Answers

2
votes

solved this by using ANDing operation:

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

This is the solution for Swift 3:

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