BLE中writeValue的方式

BLE特征属性

这里提到的特征属性在iOS的BLE中值得是CBCharacteristic类型的properties的属性,我们参考下面的代码看

/*!
 *  @enum CBCharacteristicProperties
 *
 *  @discussion Characteristic properties determine how the characteristic value can be used, or how the descriptor(s) can be accessed. Can be combined. Unless
 *              otherwise specified, properties are valid for local characteristics published via @link CBPeripheralManager @/link.
 *
 *  @constant CBCharacteristicPropertyBroadcast                     Permits broadcasts of the characteristic value using a characteristic configuration descriptor. Not allowed for local characteristics.
 *  @constant CBCharacteristicPropertyRead                          Permits reads of the characteristic value.
 *  @constant CBCharacteristicPropertyWriteWithoutResponse          Permits writes of the characteristic value, without a response.
 *  @constant CBCharacteristicPropertyWrite                         Permits writes of the characteristic value.
 *  @constant CBCharacteristicPropertyNotify                        Permits notifications of the characteristic value, without a response.
 *  @constant CBCharacteristicPropertyIndicate                      Permits indications of the characteristic value.
 *  @constant CBCharacteristicPropertyAuthenticatedSignedWrites     Permits signed writes of the characteristic value
 *  @constant CBCharacteristicPropertyExtendedProperties            If set, additional characteristic properties are defined in the characteristic extended properties descriptor. Not allowed for local characteristics.
 *  @constant CBCharacteristicPropertyNotifyEncryptionRequired      If set, only trusted devices can enable notifications of the characteristic value.
 *  @constant CBCharacteristicPropertyIndicateEncryptionRequired    If set, only trusted devices can enable indications of the characteristic value.
 *
 */
typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {
    CBCharacteristicPropertyBroadcast                                               = 0x01,
    CBCharacteristicPropertyRead                                                    = 0x02,
    CBCharacteristicPropertyWriteWithoutResponse                                    = 0x04,
    CBCharacteristicPropertyWrite                                                   = 0x08,
    CBCharacteristicPropertyNotify                                                  = 0x10,
    CBCharacteristicPropertyIndicate                                                = 0x20,
    CBCharacteristicPropertyAuthenticatedSignedWrites                               = 0x40,
    CBCharacteristicPropertyExtendedProperties                                      = 0x80,
    CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(10_9, 6_0)   = 0x100,
    CBCharacteristicPropertyIndicateEncryptionRequired NS_ENUM_AVAILABLE(10_9, 6_0) = 0x200
};

所以如何使用characteristicWriteType通过如下代码即可控制

// 通过characteristic判断
if (self.characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse) {
    self.characteristicWriteType = CBCharacteristicWriteWithoutResponse;
}
if (self.characteristic.properties & CBCharacteristicPropertyWrite) {
    self.characteristicWriteType = CBCharacteristicWriteWithResponse;
}

此处我们以一个设备的特征进行举例,特征具体为

<CBCharacteristic: 0xxxxxxxxx, UUID = XXXX, properties = 0x18, value = <aaxxxxx0 00xxxx00 xx>, notifying = YES>

然后我们拿到properties为0x18,我们进行特征计算得到如下效果:

>>> 0x18 & 4
0
>>> 0x18 & 8
8

所以当前设备需要使用CBCharacteristicPropertyWrite而不是without的WriteValue方式

欢迎留言

avatar
  Subscribe  
Notify of