我正在开发管理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.")
如何解决它没有找到任何解决方案?请帮忙。
很明显,外设端设置的权限不允许读写。您的iOS代码可能没有任何问题。
请再次检查外围GATT数据库,看它是否正确定义。