迅速 - 为什么不工作我的信号量?

时间:2017-03-14 05:23:49

标签: ios swift3 bluetooth-lowenergy semaphore

class BLEManager {
    private let bleManagerCB = BLEManagerCB()

    func checkPowered() -> Bool {
        let semaphore = DispatchSemaphore(value: 0)
        var state : Bool?

        bleManagerCB.checkPowered()
        bleManagerCB.onCompletionPowered = { result in
            state = result
            self.semaphore.signal()
        }
        semaphore.wait()
        return state
    }
}
class BLEManagerCB:NSObject, CBCentralManagerDelegate {

    var centralManager:CBCentralManager!
    var onCompletionPowered:((_ result:Bool) -> Void)?

    func checkPowered() {
        centralManager = CBCentralManager(delegate: self, queue: nil)
        //centralManagerDidUpdateState(delegate method) call.
    }

    //check power
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == .poweredOn {
            onCompletionPowered!(true)

        } else if central.state == .poweredOff {
            onCompletionPowered!(false)
        }
    }
}

嗨,我对信号量有疑问。请看我的代码。 为什么我的信号量不起作用? 这是关闭代码的问题? 我想检查功能'是同步活动的。

0 个答案:

没有答案