【发布时间】:2019-04-26 05:58:56
【问题描述】:
我正在开发管理 BLE 设备的应用程序。
我正在尝试在didDiscoverCharacteristicsFor 方法中写入数据,如下所示。
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if let characteristics = service.characteristics {
for characteristic in characteristics {
if characteristic.uuid == CBUUID(string: Common.PinUUID) {
var varInt = 0
let data = Data.init(bytes: &varInt, count: 1);
peripheral.writeValue(data, for: characteristic, type: .withResponse)
}
}
}
当我尝试在特征上写入数据时,出现如下错误。
<CBCharacteristic: 0x283b213e0, UUID = 47E9EE30-47E9-11E4-8939-164230D1DF67, properties = 0x8, value = (null), notifying = NO> - Optional("Writing is not permitted.")
我的特点是只写类型。我不知道为什么它会显示这样的错误。
此外,当我尝试读取数据时,我会像下面这样调用。
if characteristic.uuid == CBUUID(string: Common.TemperatureDataUUID) {
print(characteristic)
peripheral.readValue(for: characteristic)
}
我收到如下错误,
<CBCharacteristic: 0x283e77300, UUID = 47E9EE2B-47E9-11E4-8939-164230D1DF67, properties = 0xA, value = (null), notifying = NO> - Optional("Reading is not permitted.")
如何解决它没有找到任何解决方案?请帮忙。
【问题讨论】:
-
我之前在将 MI 手环 3 与 iOS 设备连接时遇到了同样的问题。通过在访问数据之前将身份验证设置为蓝牙设备来解决问题。我会尽快分享示例代码
-
您好,当我将数据从一台连接的设备发送到另一台设备时,我也遇到了问题。 func peripheral(_ peripheral: CBPeripheral, didWriteValueFor features: CBCharacteristic, error: Error?) { 我收到错误域=CBATTErrorDomain Code=3“不允许写入。” UserInfo={NSLocalizedDescription=不允许写入。某些设备工作正常,数据已发送,但其他设备未收到任何响应以及通知。如何在其他设备中读取相同的数据。@Vikas @ COVID19
标签: ios swift bluetooth-lowenergy core-bluetooth