如果特性支持通知,您可以使用该行关闭通知:
peripheral.setNotifyValue(true, for: characteristic)
将具有该特征的外围设备的委托设置为正确的文件非常重要,在我的例子中是:
peripheral.delegate = self
之后,当某些内容发生变化时,外围设备将在您的代码中触发此方法:
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
//data is in characteristic.value
}
如果您的特性不支持通知,您可以尝试将响应类型设置为 .withReponse 的数据写入外设,如下所示:
peripheral.writeValue(data, for: characteristic, type: .withResponse)
请记住也设置外设委托,之后外设将在每次成功写入后触发此方法:
//write response
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
//data is in characteristic.value
}