diff --git a/WBCore/WBManager.swift b/WBCore/WBManager.swift index 48ac616..c5526b6 100644 --- a/WBCore/WBManager.swift +++ b/WBCore/WBManager.swift @@ -181,6 +181,27 @@ open class WBManager: NSObject, CBCentralManagerDelegate, WKScriptMessageHandler } device.triage(view) case .requestDevice: + if (centralManager.state != CBManagerState.poweredOn) { + transaction.resolveAsFailure(withMessage: "Bluetooth not activated.") + stopScanForPeripherals() + requestDeviceTransaction = nil + clearState() + + let alert = UIAlertController(title: NSLocalizedString("turn_on_bluetooth_title", comment: "Turn on bluetooth."), message: NSLocalizedString("turn_on_bluetooth_body", comment: "Please turn on bluetooth."), preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) + + // Find top most ViewController. + let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first + if var topController = keyWindow?.rootViewController { + while let presentedViewController = topController.presentedViewController { + topController = presentedViewController + } + + topController.present(alert, animated: true) + } + + return + } guard transaction.key.typeComponents.count == 1 else { transaction.resolveAsFailure(withMessage: "Invalid request type \(transaction.key)")