I want to create a method startCentralManager
that waits until the delegate method centralManagerDidUpdateState
is called and returns a boolean value depending on the central state.
I want to use the method like this:
let centralManager = CentralManager()
let ready = centralManager.startCentralManager()
The CentralManager class should look something like this
class CentralManager: NSObject, CBCentralManagerDelegate {
var centralManager: CBCentralManager?
func startCentralManager() -> Bool {
centralManager = CBCentralManager.init(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(central: CBCentralManager) {
// startCentralManager() should return this
return central.state == CBCentralManagerState.PoweredOn
}
}
How can I solve this?
Thank you very much, in advance.
UPDATE:
There are some other methods, but the situation is almost equal. Another method is the following:
let connected = centralManager.connect<PARAMS>)
In this method if have to call
func scanForPeripheralsWithServices(_ serviceUUIDs: [CBUUID]?, options options: [String : AnyObject]?)
and wait until the delegate method
func centralManager(_ central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData advertisementData: [String : AnyObject], RSSI RSSI: NSNumber)
is called. Here, I have to call
func connectPeripheral(_ peripheral: CBPeripheral, options options: [String : AnyObject]?)
and again wait until the delegate method
func centralManager(_ central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral)
is called. In case of success, the call to centralManager.connect(<PARAMS>)
should return true
.