等待快速返回功能内的变量更改

时间:2017-03-21 09:51:30

标签: ios swift bluetooth-lowenergy

我有一个BLE系统,外围设备发送整个消息的一部分,我需要在处理之前等待整个消息。在href内我处理消息,每当我得到完整消息时,我设置peripheral(didUpdateValueFor characteristic:)。在这里,我遇到了问题:

flagDataAvailable = true

我已尝试使用// This function is called from another class override func get(_ data: [UInt8]) -> [UInt8] { var payloadGet = [UInt8(bitPattern: Int8(-1))] payloadGet = getOrPut(data, false) return payloadGet } func getOrPut(_ data: [UInt8], _ isPut: Bool) -> [UInt8] { var command = [UInt8]() var payload = [UInt8]() if isPut { command = CommunicationDevice.buildPutDataApdu(data) } else { command = CommunicationDevice.buildGetDataApdu(data) } print("Sending command \(command)") bluetoothDevice.writeValue(Data(command), for: notifyCharacteristic, type: CBCharacteristicWriteType.withResponse) let resultLength = read() let statusWord: [UInt8] = [rxBuffer[resultLength - 2], rxBuffer[resultLength - 1]] if (CommunicationDevice.TRANSMISSION_OK == statusWord) { payload = Array(rxBuffer[0..<resultLength - 2]) } return payload } func read() -> Int { // Here I need to wait until flagDataAvailable is true before continuing var nbBytes = 0 flagDataAvailable = false nbBytes = rxBufferLength rxBufferLength = 0 return nbBytes } ,但之后我从未收到针对特征更新的回调。

澄清:

flagDataAvailable的更改与需要跟踪它的函数在同一个类中进行。我的问题是如何在没有阻塞线程的情况下等待更改,也没有在有可用数据之前返回函数read()

0 个答案:

没有答案