How to Get the Status of Bluetooth (On/Off) in iPhone Programmatically

Checking Bluetooth Status

Unfortunately (stupidly?) there is no way to do it. Sorry. If you want to use the private SDK methods linked to in one of the comments to your question you certainly can, but Apple statically analyze code at the point of submission, so it's unlikely you'd be able to get it through the approval process.

I would strongly recommend you file a feature request to Apple asking for that ability in a future update. You used to be able to check for Bluetooth using GameKit, but that ability was removed when GameKit started supporting WiFi connections as well as Bluetooth.

Is there any way to switch on/off bluetooth in iPhone programmatically?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.

#if TARGET_IPHONE_SIMULATOR
exit( EXIT_SUCCESS ) ;
#else
/* this works in iOS 4.2.3 */
Class BluetoothManager = objc_getClass( "BluetoothManager" ) ;
id btCont = [BluetoothManager sharedInstance] ;
[self performSelector:@selector(toggle:) withObject:btCont afterDelay:1.0f] ;
#endif
return YES ;
}

#if TARGET_IPHONE_SIMULATOR
#else
- (void)toggle:(id)btCont
{
BOOL currentState = [btCont enabled] ;
[btCont setEnabled:!currentState] ;
[btCont setPowered:!currentState] ;

}
#endif

the above lines of code i got from here

How can I detect is the Bluetooth of the user's iPhone Off or On?

  1. You should be implementing the protocol CBPeripheralManagerDelegate, so replace CBManager in your class definition line with CBPeripheralManagerDelegate.

  2. In Swift 3, the signature of peripheralManagerDidUpdateState is now:

    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager)
  3. You can't initialize the CBPeripheralManager at class creation time since self is only available after the class has been initialized. Instead, make your property:

    var myBTManager: CBPeripheralManager?

    and initialize it in viewDidLoad:

    override func viewDidLoad() {
    ...
    myBTManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
    ...
    }

Open Bluetooth settings programmatically from UIAlertController

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.

Warning

As @user3620372 wrote, using prefs urls can cause app rejection with:

Your app uses the "prefs:root=" non-public URL scheme, which is a
private entity. The use of non-public APIs is not permitted on the App
Store because it can lead to a poor user experience should these APIs
change.

Check Bluetooth status - Swift 4

Implement CBCentralManagerDelegate delegate for that.

 var manager:CBCentralManager!

viewDidLoad() { // Or init()
manager = CBCentralManager()
manager.delegate = self
}

Delegate method :

func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .poweredOn:
break
case .poweredOff:
print("Bluetooth is Off.")
break
case .resetting:
break
case .unauthorized:
break
case .unsupported:
break
case .unknown:
break
default:
break
}
}

Check if Bluetooth is Enabled?

The only way I've ever found to do this is with private frameworks (like Bluetooth Manager, for one) that are only useful for Jailbroken apps... and Apple will reject any app using a private framework. I believe it's even against their ToS to do anything with bluetooth, so you're out of luck there.



Related Topics



Leave a reply



Submit