I've only tested this on iOS 9 so maybe someone could test this one older OS devices.
We do everything normally except one thing, instead of settings the CBCentralManager
Delegate in viewDidLoad
we leave this until the moment we need it, in the example case below I call this once my WKWebView
has finished loading, and because each page of my web view potentially requires the use of Bluetooth I put this in WKWebView didFinishNavigation
.
Swift
var managerBLE: CBCentralManager?
func bluetoothStatus() {
managerBLE = CBCentralManager(delegate: self, queue: nil, options: nil)
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
bluetoothStatus()
}
func centralManagerDidUpdateState(central: CBCentralManager) {
switch managerBLE!.state
{
case CBCentralManagerState.PoweredOff:
print("Powered Off")
case CBCentralManagerState.PoweredOn:
print("Powered On")
case CBCentralManagerState.Unsupported:
print("Unsupported")
case CBCentralManagerState.Resetting:
print("Resetting")
fallthrough
case CBCentralManagerState.Unauthorized:
print("Unauthorized")
case CBCentralManagerState.Unknown:
print("Unknown")
default:
break;
}
}
The moment that delegate is set within bluetoothStatus()
you will see the state change fire.
The notification to turn on Bluetooth only seems to want to be called right at the initial load of your app, doing it this way mean you just get what you want from the centralManagerDidUpdateState