简体   繁体   中英

How to show an alert when turn off bluetooth in Swift app?

The question could be duplicated, but I have tried a lot of code and it wasn't work. I have read about

CBCentralManagerOptionShowPowerAlertKey

option and the possibility to show an alert to go settings bluetooth option into iPhone for turn on on the application, but I haven't get works this.

If I set the key to true the key not appear, and when I change the value to false the key not works...

I'm using this code into my Swift application:

var bCentralManger: CBCentralManager!
self.bCentralManger = CBCentralManager(delegate: self, queue: DispatchQueue.main, options: [CBCentralManagerOptionShowPowerAlertKey: true])

Works this option in iOS? I'm using iOS 12 on my device. Have anybody this code working? Thanks!

The Bluetooth power alert is only shown if Bluetooth is turned off from the Settings app. If you simply disable Bluetooth from the control center, then the dialog is not shown. Disabling Bluetooth from the control center disconnects any connected peripherals but does not completely power off the Bluetooth hardware.

You can display your own alert in response to a .poweredOff state in the centralManagerDidUpdateState delegate method. Unfortunately you cannot send the user directly to the Bluetooth settings in this case.

You could log a Radar with Apple regarding this behaviour, but I suspect that they will respond that the system is working as intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM