Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a CBMutableCharacteristic with multiple CBCharacteristicProperties and Permissions

I'm creating a new CBMutableCharacteristic for use in a Bluetooth app I'm making. I got some code from a tutorial, which looks like this:

_customCharacteristic = [[CBMutableCharacteristic alloc] initWithType:characteristicUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];

where _customCharacteristic is my CBMutableCharacteristic.

However, I want to initialize my _customCharacteristic with other properties, such as CBCharacteristicPropertyRead and CBCharacteristicPropertyWrite. The same is true for the permissions: I want to also give it CBAttributePermissionsWriteable.

According to this: http://developer.apple.com/library/ios/#documentation/CoreBluetooth/Reference/CBCharacteristic_Class/translated_content/CBCharacteristic.html#//apple_ref/doc/c_ref/CBCharacteristicProperties

and this: http://developer.apple.com/library/ios/#documentation/CoreBluetooth/Reference/CBMutableCharacteristic_Class/Reference/CBMutableCharacteristic.html#//apple_ref/doc/c_ref/CBAttributePermissions

I can have both multiple properties and permissions for each characteristic. However, I don't know how to initialize my CBMutableCharacteristic in this way.

like image 340
Graham Carling Avatar asked Aug 07 '13 21:08

Graham Carling


3 Answers

Its an enum, whose different values can be ORed bitwisely, so you can use the characteristic properties and permissions together:

CBMutableCharacteristic *_customCharacteristic = [[CBMutableCharacteristic alloc] initWithType:characteristicUUID properties:CBCharacteristicPropertyNotify+CBCharacteristicPropertyRead
 value:nil permissions:CBAttributePermissionsReadable|CBAttributePermissionsWriteable];
like image 118
Tommy Devoy Avatar answered Oct 17 '22 20:10

Tommy Devoy


tdevoy answer is right one , Or following is also working code for me

characteristic = [[CBMutableCharacteristic alloc] initWithType:characteristicUUID      properties:CBCharacteristicPropertyWriteWithoutResponse|CBCharacteristicPropertyRead  value:nil permissions:CBAttributePermissionsReadable|CBAttributePermissionsWriteable];
like image 31
V-Xtreme Avatar answered Oct 17 '22 21:10

V-Xtreme


The current answers were valid at the time, but it's been eight years and now Apple uses an OptionSet for the properties and permissions.

The following would be the modern, Swift based approach for initializing a CBMutableCharacteristic, and includes a short sample snippet you can run in a playground to verify.

import CoreBluetooth

let cbuuid = CBUUID()
let characteristic = CBMutableCharacteristic(type: cbuuid,
                                             properties: [.notify, .read],
                                             value: nil,
                                             permissions: [.readable, .writeable])
// All print true
print(characteristic.properties.contains(.read))
print(characteristic.properties.contains(.notify))
print(characteristic.permissions.contains(.readable))
print(characteristic.permissions.contains(.writeable))
like image 1
CodeBender Avatar answered Oct 17 '22 20:10

CodeBender