iOS:写描述符不允许写

时间:2018-07-15 08:23:47

标签: ios swift bluetooth-lowenergy

我正在为中央和外围示例构建简单的

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

但是当iOS充当外围设备时,我无法编写描述符,

我得到错误域= 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))")
    }

1 个答案:

答案 0 :(得分:2)

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

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

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