CoreBluetooth XPC connection invalid
Asked Answered
P

5

41
public class BLE: NSObject, CBCentralManagerDelegate {

    var centralManager:CBCentralManager!

    public override init() {
        super.init()
        self.centralManager = CBCentralManager.init(delegate: self, queue: nil)
    }

    public func centralManagerDidUpdateState(_ central: CBCentralManager) {

        switch central.state {
        case .unknown:
            print("unknown")
        case .resetting:
            print("resetting")
        case .unsupported:
            print("unsupported")
        case .unauthorized:
            print("unauthorized")
        case .poweredOff:
            print("powered off")
        case .poweredOn:
            print("powered on")
            self.centralManager.scanForPeripherals(withServices: nil, options: nil)
        }
    }
}

This is my code, whenever I run it, it gives me the message

“[CoreBlueooth] XPC Connection Invalid”

I did try adding NSBluetoothPeripheralUsageDescription into my info.plist file but that didn’t work.

The weird part though is that, if I initialize CBCentralManager directly instead of using a class then everything works fine.

This problem only arises when I try to initialize CBCentralManager by creating on object of the class BLE or any other class for that matter.

Potty answered 19/4, 2018 at 9:35 Comment(5)
Have you check this url. #43880846Katmandu
you have to add some key in your info.plist will resolve this issueKatmandu
Try defining CBCentralManager in your appDelegate, use same CBCentralManager in your BLE classKatmandu
I already came across that url before I asked my question which is why I tried editing my info.plist in the first place. Perhaps you could be more specific as to which keys I should add?Potty
I’ve also tried defining CBCentralManager in my appDelegate, no luck unfortunatelyPotty
S
36

CBCentralManager reference should be a strong reference to the class as a member variable. It cannot work as a local reference.

Try next:

class ViewController: UIViewController {
   var ble: BLE!
   override func viewDidLoad() {
      super.viewDidLoad()

      ble = BLE()
  }
}

class BLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
   private var manager: CBCentralManager!

   required override init() {
      super.init()
      manager = CBCentralManager.init(delegate: self, queue: nil)
   }

   func centralManagerDidUpdateState(_ central: CBCentralManager) {
      var consoleLog = ""

      switch central.state {
      case .poweredOff:
          consoleLog = "BLE is powered off"
      case .poweredOn:
          consoleLog = "BLE is poweredOn"
      case .resetting:
          consoleLog = "BLE is resetting"
      case .unauthorized:
          consoleLog = "BLE is unauthorized"
      case .unknown:
          consoleLog = "BLE is unknown"
      case .unsupported:
          consoleLog = "BLE is unsupported"
      default:
          consoleLog = "default"
      }
      print(consoleLog)
   }
}
Sweep answered 7/12, 2018 at 11:34 Comment(0)
T
20

I ran into the same issue: sandboxing is "On" by default and disables access to Bluetooth.

Make sure your target capabilities allow Bluetooth HW access (see attached screenshot). Target capabilities

Thumbprint answered 29/8, 2018 at 17:58 Comment(4)
I didn't see that app sandbox section eitherMunniks
Look at Background ModesFlattie
This is no longer valid in XCode.Harridan
Thanks! Worked for me in Xcode 11 - it's under "Signing & Capabilities" nowAnabolism
C
13

For people who are running an iOS simulator, bluetooth does not work within it. Thus, if you try and simulate an app using bluetooth, it will raise the "[CoreBlueooth] XPC Connection Invalid" error.

For further information : https://www.browserstack.com/test-on-ios-simulator

You need to test your app on a real device.

Crescentia answered 5/8, 2019 at 10:51 Comment(1)
Obvious, but easy to forget. Thanks.Usable
H
3

In my case it turns out that I was making a call for Scan too soon. I was making the call from the ViewDidLoad method. The scan would work fine if I initiated it via the press of a button.

The work around I used was to use the performSelector with a delay when calling the scan from the ViewDidLoad method.

Harridan answered 29/8, 2019 at 19:3 Comment(0)
C
-4

I solved changing NSObject to UIViewController in my manager class

Carry answered 26/11, 2019 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.