Ios 在Swift中轮询来自BLE适配器的响应

Ios 在Swift中轮询来自BLE适配器的响应,ios,swift,swift2,bluetooth-lowenergy,cbcentralmanager,Ios,Swift,Swift2,Bluetooth Lowenergy,Cbcentralmanager,使用基于Wi-Fi套接字的适配器,我可以成功轮询以下响应: func writeMessageWithResponse(message: String) -> [String] { self.waitingForResponse = true let runLoop = NSRunLoop.currentRunLoop() if self.response != nil { self.response?.rem

使用基于Wi-Fi套接字的适配器,我可以成功轮询以下响应:

    func writeMessageWithResponse(message: String) -> [String] {
        self.waitingForResponse = true
        let runLoop = NSRunLoop.currentRunLoop()
        if self.response != nil {
            self.response?.removeAll()
        }

        writeMessage(message) // this will set self.waitingForResponse to false when a response is received

        while self.waitingForResponse && runLoop.runMode(NSDefaultRunLoopMode, beforeDate: NSDate.distantFuture()) {
             // waiting for flag (self.waitingForResponse) to be set
        }

        return self.response!
    }
当我在
CBCentralManager
BLE连接中使用相同的代码时,主线程被阻塞,并且没有收到来自连接的响应。我尝试将
CBCentralManager
更改为不同的队列,但得到了相同的结果

有没有人知道如何在循环中等待,并且仍然能够收到可接受的响应?我知道正在发送响应,但线程被阻止,无法读取


使用带有completionHandler的异步函数不适用于此用例,因为我需要一个可重用的函数,该函数可以发出一系列命令,每个命令都取决于上次响应的结果。

CBCentralManager和CBPeripheral API并不是为此而设计的。您应该使用CBPeripheralDelegate和CBCentralManagerDelegate协议提供的方法。使用专用队列初始化中央管理器,以便您可以侦听对外围设备的响应,并根据需要对该数据进行操作

投票很少是个好主意;尤其是在以电池供电的用户为中心的移动环境中。您肯定需要查看某种命令队列,以便可以使用异步响应触发下一个命令。这可能像数组一样简单,也可能像NSOperation objectsThank这样更复杂。我目前正在使用一个NSO操作,但我现在正在尝试扩展它以包括完成处理程序。很抱歉,我没有说清楚。该函数位于扩展CBPeripheralDelegate和CBCentralManagerDelegate的类中。问题是,在循环中等待时没有收到响应,但在循环被放弃后会通过。我现在正试图更改代码以使用完成处理程序异步工作。