Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreBluetooth XPC connection invalid

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.

like image 813
Zeryx Avatar asked Apr 19 '18 09:04

Zeryx


2 Answers

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)    } } 
like image 149
Belzik Avatar answered Sep 16 '22 12:09

Belzik


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

like image 31
zenerino Avatar answered Sep 16 '22 12:09

zenerino