Paul T.
Paul T.

Reputation: 5038

iOS issue checking if Bluetooth is ON

I need to check if Bluetooth is On.

I use this code:

func startDetectingBluetoothState() {
        if self.centralManager == nil {
            self.centralManager = CBCentralManager(delegate: self, queue: self.workingQueue, options: [CBCentralManagerOptionShowPowerAlertKey: false])
        }
    }

func centralManagerDidUpdateState(_ central: CBCentralManager) {
        let state = central.state
        DispatchQueue.main.async { [weak self] in
            // notify observers about state change
            self?.stateObservers.invokeDelegates { stateObserver in
                stateObserver.bluetoothStateChanged()
            }
        }
    }

I start the app on iPhone X 11.3 (Bluetooth is On in the Settings), then almost immidiatly it enters in centralManagerDidUpdateState(_ central: CBCentralManager) with state == .poweredOff. So the value is wrong. Then I turn off Bluetooth in Settings, centralManagerDidUpdateState(_ central: CBCentralManager) function is not called (because the previous state it detected was .poweredOff), then I turn on Bluetooth and centralManagerDidUpdateState(_ central: CBCentralManager) is called with .poweredOn value. Then it detects every state change properly. But if I restart the app and Bluetooth is On, it can't detect it again. If during start the Bluetooth is Off, everything is OK.

I have another device iPhone 6+ 11.2.5, where everything works as a charm :)

Upvotes: 1

Views: 4717

Answers (2)

Vadlapalli Masthan
Vadlapalli Masthan

Reputation: 176

Use this Code Working for you

 var centralManager:CBCentralManager!

 override func viewDidLoad()
{
    super.viewDidLoad()

    // This will trigger centralManagerDidUpdateState
    centralManager = CBCentralManager(delegate: self, queue: nil)

}

 func centralManagerDidUpdateState(_ central: CBCentralManager)
{
    print("Central Manager did update state")

    if (central.state == .poweredOn)
    {

        self.centralManager?.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
    }
    else 
     {
        // Bluetooth is unavailable for some reason

        // Give feedback
        var message = String()
        switch central.state
        {
        case .unsupported:
            message = "Bluetooth is unsupported"
        case .unknown:
            message = "Bluetooth state is unkown"
        case .unauthorized:
            message = "Bluetooth is unauthorized"
        case .poweredOff:
            message = "Bluetooth is powered off"
        default:
            break
        }
        print(message)

        UIAlertView(
            title: "Bluetooth unavailable",
            message: message,
            delegate: nil,
            cancelButtonTitle: "OK")
            .show()
    }
}

Upvotes: 0

Abhishek
Abhishek

Reputation: 3584

You need to implement CBCentralManagerDelegate. Below is the sample code which might help you to check the condition -

 var centralManager:CBCentralManager!

 in init() or viewDidLoad()
 {
     centralManager = CBCentralManager()
     centralManager.delegate = self
 }

func centralManagerDidUpdateState(_ central: CBCentralManager) {

  if central.state == .poweredOn {
     print("Bluetooth is connected")
  }
  else if central.state == .poweredOff{
     print("Bluetooth is not Connected.")
  }
}

Upvotes: 3

Related Questions