CoreBluetooth peripheral.setNotifyValue(true,characteristic)未通知

时间:2015-01-31 18:59:58

标签: ios swift core-bluetooth

我有两个程序,一个用于Mac,一个用于iOS。当我从Mac连接到iOS设备时,它会找到我想要的服务以及我想要的特性。找到特征后,我使用peripheral.setNotifyValue(true, forCharacteristic: characteristic),但是在iOS端没有调用peripheralManager:central:didSubscribeToCharacteristic:方法。当我检查characteristic.isNotifying是否为假时。根据我的理解,当我将notify值设置为true时,它应该是通知,每当我更改值时,都是更新。为什么不更新?提前谢谢。

以下是设置相关特征的代码 -

self.characteristic = CBMutableCharacteristic(type: UUID_CHARACTERISTIC, properties: CBCharacteristicProperties.Read, value: self.dataToSend, permissions: CBAttributePermissions.Readable) 
theService = CBMutableService(type: UUID_SERVICE, primary: true)
theService.characteristics = [characteristic]
self.peripheralManager.addService(theService)

1 个答案:

答案 0 :(得分:0)

如果您希望您的特性支持notify操作,那么您需要在其属性上设置它 -

self.characteristic = CBMutableCharacteristic(type: UUID_CHARACTERISTIC, properties: CBCharacteristicProperties.Read|CBCharacteristicProperties.Notifiy, value: self.dataToSend, permissions: CBAttributePermissions.Readable);

Core Bluetooth会忽略尝试在未声明支持通知的特性上设置通知。

另外,请注意,通过在创建特征时设置值,您将来无法更改此特征的值 - 因此通知有点无意义。如果您希望能够更改值,则必须在创建特征时为nil指定value

来自CBMutableCharacteristic文档 -

  

讨论

     

如果为特征指定值,则值为   缓存,其属性和权限设置为   CBCharacteristicPropertyRead和CBAttributePermissionsReadable,   分别。因此,如果你需要一个特征的值   是可写的,或者如果你期望在生命周期中价值发生变化   对于特征所属的已发布服务,您必须   将值指定为nil。这样做可以确保价值   每当时动态处理并由外围设备管理器请求   外围设备管理器从a接收读或写请求   中央。当外围设备管理器收到读或写请求时   从中心,它调用peripheralManager:didReceiveReadRequest:   或者peripheralManager:didReceiveWriteRequests:它的方法   委托对象,分别。

相关问题