Home Articles

更新CBCentral的特征值

Asked
Viewed 1218 times
0

我已成功设法让我的iOS应用程序(外围设备)向我的OSX应用程序(中央)发送消息 .

在尝试让Central更新特征值并从外围设备读取时,我现在遇到了问题 .

如果我将CBCharacteristicProperties更改为Notify以外的任何其他内容,我的中心无法订阅该特征,并出现以下错误:“不允许写入” .

self.transferCharacteristic = CBMutableCharacteristic(type: CBUUID.UUIDWithString(TRANSFER_CHARACTERISTIC_UUID), properties: CBCharacteristicProperties.Read, value: nil, permissions: CBAttributePermissions.Writeable)

根据我的理解,为了必须对中心写入外设,我应该在外设中使用上述行的组合,这个事件:

func peripheralManager(peripheral: CBPeripheralManager!, didReceiveWriteRequests requests: [AnyObject]!) {}

这在中央更新特征的 Value :

self.discoveredPeripheral?.writeValue(passwordData, forCharacteristic: self.characteristicSubscribed, type: CBCharacteristicWriteType.WithoutResponse)

请让我知道我做错了什么 . 任何展示如何做我想做的示例项目都将受到高度赞赏 .

谢谢

1 Answer

  • 1

    您需要通过组合枚举值将属性标记为可读和可写 -

    var cbProperties = CBCharacteristicProperties.Read|CBCharacteristicProperties.Write
    var cbPermissions = CBAttributePermissions.Readable|CBAttributePermissions.Writeable
    
    var transferCharacteristic = CBMutableCharacteristic(type: CBUUID.UUIDWithString(TRANSFER_CHARACTERISTIC_UUID), properties: cbProperties, value: nil, permissions: cbPermissions)
    

Related