7

I am working on a BLE project, for that I initiate CBCentralManager, and scan for available devices. I am able to scan and connect with available BLE peripheral device. Everything is working fine till I have not tested this on iOS 11.0.x version.
when I tested on iOS 11.1.1 or 11.1.2, CBCentralManager always return me poweredOff state, when launch app. But when I open control center, and turn off and turn on bluetooth again or activate/deactivate FlightMode (automatically turn off/on bluetooth). App start scanning BLE peripherals and everything looks fine, till restart on app. Does anyone faced such issue on iOS 11.1.x and able to fix this,Please help to fix this.

Below is my code to check status

func initiateCentralManager(){
      manager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey : "MyBLECurrentState"]) 
}

 func centralManagerDidUpdateState(_ central: CBCentralManager){
       print("Received CBCentralManager state")
        peripheralArray.removeAll()
        if central.state == .poweredOn {
            print("poweredOn")
        } else if central.state == .poweredOff {
            print("poweredOff")

        }
    }
Surjeet Singh
  • 11,691
  • 2
  • 37
  • 54

1 Answers1

17

Here is what is happening

iOS has 3 levels of Bluetooth status:

  1. Bluetooth ON and New Connections Allowed
  2. Bluetooth ON and New Connections Not Allowed
  3. Bluetooth OFF

CBCentralManager will return poweredOff state on cases 2 and 3

To be in state 1 (Bluetooth ON and New Connections Allowed)

  • switch OFF Bluetooth in settings
  • switch ON in Control Center

To be in state 2 (Bluetooth ON and New Connections Not Allowed)

  • switch OFF Bluetooth in settings
  • switch ON in Control Center
  • switch OFF in Control Center (the button will change to white)

To go in state 1 from 2

  • go it BT setting and click "Allow New Connections"
antoinem
  • 503
  • 4
  • 12
  • 2
    How to determine in code by CentralManager.state if state is in case 2 or case 3? @antoinem? – Nguyễn Trọng Bằng May 30 '18 at 11:07
  • you can't that's the problem... best solution is to suggest your users to activate Bluetooth from control center – antoinem Jun 08 '18 at 04:21
  • I'm getting into `poweredOff` state by this: switch OFF in Control Center -> switch OFF Bluetooth in settings -> switch ON Bluetooth in settings -> restart application. Bluetooth shown as ON EVERYWHERE but when status checked inside application it says `poweredOff` – Anton Plebanovich Sep 20 '18 at 10:41
  • For user to fix it he should OFF and ON bluetooth in control center. It looks like iOS bug that bluetooth state aren't show correctly in control center. Switching bluetooth OFF and ON in device settings will only work until app is not restarted so bug will appear again on each app restart in that case. – Anton Plebanovich Sep 20 '18 at 10:54