Enable/Disable bluetooth programmatically using CoreBluetooth
I want to enable/disable Bluetooth programmatically without using private API (which will be rejected by App Store). I just see CoreBluetooth can get current bluetooth state. Can I archive it with CoreBluetooth, I cannot find out any relevant information to that. Thanks for any advanced help.
4 Answers 4
You cannot enable or disable the Bluetooth radio through an app. You can disable your own app’s use of Bluetooth but the Bluetooth radio will still be enabled.
Only the user can enable/disable Bluetooth through settings.
You cannot switch On/Off the bluetooth from your app as it is not available in the Core Bluetooth framework. You can simply pull out the Notification centre to switch it ON and Off which can easily serve your purpose.
Yes, you can use Core Bluetooth Framework for this.
Just import the Bluetooth Manager Header file and write the following code on button click:
[btManager setPowered:YES]; [btManager setEnabled:YES];
In this example, btManager is an object of Bluetooth Manager class.
This is a private API which will get your app rejected from the App Store. Check out here: stackoverflow.com/questions/1743610/…
I haven’t seen any change in documentation that allow user to switch on/off bluetooth. The best it can do, it show the system pop-up to go to settings by calling CoreBluetooth methods. All other way should be for jailbroken devices or using private API that should get you rejected from AppStore Reviews
How to switch bluetooth on/off using Swift 3?
I am new to Swift, and trying to learn communicating with bluetooth and wifi. I went through many online tutorials but they seems only teaching how to initiate centralDeviceManager and scan and connect to a device, and also checking for the status, but I can’t find out how to enable/disable bluetooth. I got this in my viewController.swift file. Can anyone please suggest me how to enable it please.
import CoreBluetooth class ProfileListViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,CBPeripheralDelegate,CBCentralManagerDelegate < var manager:CBCentralManager! var peripheral:CBPeripheral! let BEAN_NAME = "Robu" let BEAN_SCRATCH_UUID = CBUUID(string: "bb2819df-f8ef-4f90-811e-20a4f53eb949") let BEAN_SERVICE_UUID = CBUUID(string: "bb2819df-f8ef-4f90-811e-20a4f53eb949") func centralManagerDidUpdateState(_ central: CBCentralManager)< print("CentralManager is initialized") switch central.state< case CBManagerState.unauthorized: print("The app is not authorized to use Bluetooth low energy.") case CBManagerState.poweredOff: print("Bluetooth is currently powered off.") case CBManagerState.poweredOn: print("Bluetooth is currently powered on and available to use.") default:break >> >
Hi @Ramkumarchintala is this same for Wifi, Airplane mode, Mobile Data, Location Service, Do not Disturb. settings that the user can’t programmatically enable/disable them?
Thank your Piyush bhai, damn I was planning to create an app which allows user to maintain a profile with all this settings and then schedule them with time and week days to automatically enable it, like old nokia phones.
Turning on Bluetooth programmatically Swift
I am trying to turn on Bluetooth automatically using my app. The usual way would be for the user to go to settings and turn on. But I need to to be turned on from the app. I have gone through lots of documentation and they are referring to private APIs but all are very old. I do not mind that it’s not going to be approved at App Store. Is there any way to turning on Bluetooth programmatically?
You can’t turn on/off bt programmatically, but you can view the bt state and prompt the user to turn it on. Read this: stackoverflow.com/questions/23833757/…
there are private APIs which could work previously, correct? I would like to use them but most of them are outdated
2 Answers 2
Is there any way or hack to turning on Bluetooth programmatically?
There is no provision to turning on Bluetooth programmatically. We can only notify user If the user has turned off Bluetooth all you can do is display an alert or message asking them to turn it on.
Refer CBCentralManager for monitoring Bluetooth state.
To the community, sorry for the frustration but I am posting the answer myself now for devs in the future in case anyone needs to know. Just felt the community can do alot better than this, Stacksoverflow was supposed to be a community where devs help each other. IT CAN be done, however it’s by private framework which CAN BE FOUND. An example would be using BeeTee, https://github.com/michaeldorner/BeeTee . It enables bluetooth to be turned on and the usage is given there by
class Demo: BeeTeeDelegate < let beeTee = BeeTee() init() < beeTee.delegate = self beeTee.enableBluetooth() >>
Yes, but it says that «Based on the AppStore guideline §2.5 on private (undocumented) functions it is not possible to publish apps with the BeeTee and BluetoothManager.framework in the AppStore.». It worths mentioning.
Open Bluetooth settings programmatically from UIAlertController
As titled, the question is how can I open bluetooth settings programmatically from my App? I checked all answers on stackoverflow and not even one is working fine with iOS 11. All of them are old answers for is below 11 or are left without answer. All I can make is take me to general settings or app settings, but I want user to be taken directly to Bluetooth settings, so he can turn it on.
let alert = UIAlertController(title: "Bluetooth is off", message: "Please turn on your bluetooth", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Go to settings", style: UIAlertActionStyle.default, handler: < (action) in switch action.style < case .default: let url = URL(string: "App-Prefs:root=Bluetooth") //for bluetooth setting let app = UIApplication.shared app.openURL(url!) print("default") case .cancel: print("cancel") case .destructive: print("destrucive") >
UIApplication.shared.openURL(NSURL(string: "prefs:root=General&path=Bluetoth")! as URL)
let url = URL(string: "App-Prefs:root=Bluetooth")
iOS has never publicly supported any URL scheme to launching any Settings page except your own app’s page.
4 Answers 4
So after some deep research (I couldn’t believe that it is not possible) and tries with fails I can write a short answer (summary) for my question. As @maddy wrote, iOS doesn’t support URL scheme to launching any direct settings, only to General Settings (the list of all settings), or to Application Settings (your application settings (which you can implement)/permissions list). Unfortunately, for you application settings you can’t add there also any of iOS System settings to be easy accessed there. Poor..
To go to General Settings you can use:
let url = URL(string: "App-Prefs:root=General")
To go to YouApp Settings your can use:
let url = URL(string: UIApplicationOpenSettingsURLString)
And of course remember to open that URL: (Yes, I know you can do it all in one line)
let app = UIApplication.shared app.openURL(url!)
Also, as @Paulw11 mentioned, you can open bluetooth settings directly by CBCentralManagerShowPowerAlertKey option. So when you open your app and instantiate CBCentralManager the built-in alert dialog will popup, telling you that you need to «turn on bluetooth to use accessories (peripherals) with your app», with 2 buttons: Settings (that direct’s you to bluetooth settings) and OK (dismiss dialog). Unfortunately this shows up when instantiating central manager, so if you want to hold central manager for your whole app (like singleton etc) than it’s not so good as you want, cause it will popup only once. In other case that might be it..
NOW! There is something really wrong with ios bluetooth. In iOS you can turn on/off your bluetooth by 2 ways: one from Settings -> Bluetooth, and the second one by swiping from down to top and selecting bluetooth icon. BUT. Now, when you do it by second way, the bluetooth in Settings is still turned on! So even if you open your application and the bluetooth Alert Dialog will popup, and you select go to settings, you will see that bluetooth is ON.
That is really strange here on iOS..
So, in short summary, the answer for my question is:
YOU CANNOT OPEN BLUETOOTH SETTINGS PROGRAMATICALLY by your own AlertDialog. You can use built-in iOS CBCentralManager AlertDialog.
Hope I saved some of your time.
opening URL For iOS 10+ (when I changed version in my project I had to change it):
app.open(url!, options: [:], completionHandler: nil)
Bluetooth strange behavior — update
As I mentioned above, you can switch bluetooth on/off by two ways, which causes strange behavior. Now I should make myself better at this point: that was strange behavior for me (I was used to Android before). Now I know why this behavior is like this. It is because, the way of turning bluetooth by the second way I described (swiping top from down and pressing bluetooth icon) TURNS OFF NOT BLUETOOTH CONNECTION, BUT IT TURNS OFF ALLOWING CONNECTIONS (bluetooth is still turned on, but can not connect with devices (which in apps is treated as bluetooth would be turned off!!). But still you can not manage it programatically anyway.. And I still think that it should be improved by apple.
As @user3620372 wrote, using prefs urls can cause app rejection with:
Your app uses the «prefs:root mt24″>