Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CBCentralManager always return .poweredOff state on iOS 11.1.x

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")

        }
    }
like image 995
Surjeet Singh Avatar asked Nov 30 '17 07:11

Surjeet Singh


1 Answers

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"
like image 183
antoinem Avatar answered Sep 28 '22 10:09

antoinem