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)
}
}
}
嗨,我对信号量有疑问。请看我的代码。 为什么我的信号量不起作用? 这是关闭代码的问题? 我想检查功能'是同步活动的。