CoreBluetooth peripheral.setNotifyValue(true, characteristic) 没有通知
CoreBluetooth peripheral.setNotifyValue(true, characteristic) is not notifying
我有两个程序,一个用于 Mac,一个用于 iOS。当我从 Mac 连接到 iOS 设备时,它会找到我想要的服务和我想要的特性。找到特征后,我使用 peripheral.setNotifyValue(true, forCharacteristic: characteristic)
,但是 peripheralManager:central:didSubscribeToCharacteristic:
方法没有在 iOS 端被调用。当我检查 characteristic.isNotifying
是否为假时。据我了解,当我将通知值设置为 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)
如果你想让你的特性支持 notify
操作,那么你需要在它的属性上设置 -
self.characteristic = CBMutableCharacteristic(type: UUID_CHARACTERISTIC, properties: CBCharacteristicProperties.Read|CBCharacteristicProperties.Notifiy, value: self.dataToSend, permissions: CBAttributePermissions.Readable);
Core Bluetooth 会忽略在不声明支持通知的特征上设置通知的尝试。
另外,请注意,通过在创建特性时设置值,您以后将无法更改此特性的值 - 因此通知有些毫无意义。如果您希望能够更改值,则必须在创建特征时为 value
指定 nil
。
来自 CBMutableCharacteristic
文档 -
Discussion
If you specify a value for the characteristic, the value is
cached and its properties and permissions are set to
CBCharacteristicPropertyRead and CBAttributePermissionsReadable,
respectively. Therefore, if you need the value of a characteristic to
be writeable, or if you expect the value to change during the lifetime
of the published service to which the characteristic belongs, you must
specify the value to be nil. So doing ensures that the value is
treated dynamically and requested by the peripheral manager whenever
the peripheral manager receives a read or write request from a
central. When the peripheral manager receives a read or write request
from a central, it calls the peripheralManager:didReceiveReadRequest:
or the peripheralManager:didReceiveWriteRequests: methods of its
delegate object, respectively.
我有两个程序,一个用于 Mac,一个用于 iOS。当我从 Mac 连接到 iOS 设备时,它会找到我想要的服务和我想要的特性。找到特征后,我使用 peripheral.setNotifyValue(true, forCharacteristic: characteristic)
,但是 peripheralManager:central:didSubscribeToCharacteristic:
方法没有在 iOS 端被调用。当我检查 characteristic.isNotifying
是否为假时。据我了解,当我将通知值设置为 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)
如果你想让你的特性支持 notify
操作,那么你需要在它的属性上设置 -
self.characteristic = CBMutableCharacteristic(type: UUID_CHARACTERISTIC, properties: CBCharacteristicProperties.Read|CBCharacteristicProperties.Notifiy, value: self.dataToSend, permissions: CBAttributePermissions.Readable);
Core Bluetooth 会忽略在不声明支持通知的特征上设置通知的尝试。
另外,请注意,通过在创建特性时设置值,您以后将无法更改此特性的值 - 因此通知有些毫无意义。如果您希望能够更改值,则必须在创建特征时为 value
指定 nil
。
来自 CBMutableCharacteristic
文档 -
Discussion
If you specify a value for the characteristic, the value is cached and its properties and permissions are set to CBCharacteristicPropertyRead and CBAttributePermissionsReadable, respectively. Therefore, if you need the value of a characteristic to be writeable, or if you expect the value to change during the lifetime of the published service to which the characteristic belongs, you must specify the value to be nil. So doing ensures that the value is treated dynamically and requested by the peripheral manager whenever the peripheral manager receives a read or write request from a central. When the peripheral manager receives a read or write request from a central, it calls the peripheralManager:didReceiveReadRequest: or the peripheralManager:didReceiveWriteRequests: methods of its delegate object, respectively.