I need to check if Bluetooth is On.
I use this code:
func startDetectingBluetoothState() {
if self.centralManager == nil {
self.centralManager = CBCentralManager(delegate: self, queue: self.workingQueue, options: [CBCentralManagerOptionShowPowerAlertKey: false])
}
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
let state = central.state
DispatchQueue.main.async { [weak self] in
// notify observers about state change
self?.stateObservers.invokeDelegates { stateObserver in
stateObserver.bluetoothStateChanged()
}
}
}
I start the app on iPhone X 11.3
(Bluetooth is On in the Settings), then almost immidiatly it enters in centralManagerDidUpdateState(_ central: CBCentralManager)
with state == .poweredOff
. So the value is wrong. Then I turn off Bluetooth in Settings, centralManagerDidUpdateState(_ central: CBCentralManager)
function is not called (because the previous state it detected was .poweredOff), then I turn on Bluetooth and centralManagerDidUpdateState(_ central: CBCentralManager)
is called with .poweredOn
value. Then it detects every state change properly. But if I restart the app and Bluetooth is On, it can't detect it again. If during start the Bluetooth is Off, everything is OK.
I have another device iPhone 6+
11.2.5
, where everything works as a charm :)