gpt4 book ai didi

iOS:写入描述符不允许写入

转载 作者:搜寻专家 更新时间:2023-10-31 22:39:16 25 4
gpt4 key购买 nike

我正在为 Central 和 Peripheral 示例构建简单的应用程序,

中央代码工作正常,因为当 Android 充当外围设备时,我能够编写特征和描述符,

但是当iOS作为Peripheral时,我无法写描述符,

我收到错误域=CBATTErrorDomain 代码=3“不允许写入。” UserInfo={NSLocalizedDescription=不允许写入。}

我相信,我错过了一些东西,知道我做错了什么吗?

//Peripheral Code
transferCharacteristic = CBMutableCharacteristic(
type: transferCharacteristicUUID,
properties: [.indicate,.writeWithoutResponse,.read],
value: nil,
permissions: [.writeable,.readable]
)

let userDesc = CBMutableDescriptor(type: userCharacteristicUUID, value: "Used to send commands")

transferCharacteristic?.descriptors = [userDesc]

// Then the service
transferService = CBMutableService(
type: transferServiceUUID,
primary: true
)

// Add the characteristic to the service
transferService?.characteristics = [transferCharacteristic!]
peripheralManager!.add(transferService!)
peripheralManager!.startAdvertising([CBAdvertisementDataServiceUUIDsKey : [transferServiceUUID],CBAdvertisementDataLocalNameKey : "Broadcaster"])


//Central Code where it throw error
/** This callback lets us know more data has arrived via notification on the characteristic
*/
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
var clientDes : CBDescriptor?

for desc in characteristic.descriptors!{
if desc.uuid.uuidString == CBUUIDCharacteristicUserDescriptionString {
clientDes = desc
}
}
let sendValue:[UInt8] = [0x02, 0x00]
let data = NSData(bytes: sentValue, length: 2)
peripheral.writeValue(data as Data, for: clientDes!)
}

//Here I am getting the log
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?) {
print("wrote to \(String(describing: error))")
}

最佳答案

尽管 CBPeripheral 提供了 writeValue(data:Data, descriptor: CBDescriptor) 方法并且文档仅声明您不能写入 CBUUIDClientCharacteristicConfigurationString< 类型的描述符CBPeripheralManagerDelegate 不提供任何方式来通知写入描述符。

因此,您无法从 CBPeripheralManager 提供的外围设备写入任何描述符。

您应该简单地创建另一个要写入的特征。

关于iOS:写入描述符不允许写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51346622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com