CoreBluetooth XPC连接无效

Zer*_*ryx 11 xpc core-bluetooth swift

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码,每当我运行它时,它就会给我消息

“ [CoreBlueooth] XPC连接无效”

我确实尝试将NSBluetoothPeripheralUsageDescription添加到我的info.plist文件中,但这没有用。

不过,奇怪的是,如果我直接初始化CBCentralManager而不是使用类,则一切正常。

仅当我尝试通过创建BLE类或任何其他类的on对象初始化CBCentralManager时,才会出现此问题。

小智 13

我遇到了同样的问题:默认情况下沙箱是“打开”并禁用对蓝牙的访问。

确保您的目标功能允许蓝牙硬件访问(见附件截图)。 目标能力

  • 这在 XCode 中不再有效。 (2认同)
  • 谢谢!在 Xcode 11 中为我工作 - 现在位于“签名和功能”下 (2认同)

Fro*_*rch 8

对于运行 iOS 模拟器的人来说,蓝牙在其中不起作用。因此,如果您尝试使用蓝牙模拟应用程序,则会引发“[CoreBlueooth] XPC 连接无效”错误。

欲了解更多信息:https : //www.browserstack.com/test-on-ios-simulator

您需要在真实设备上测试您的应用。


Bel*_*zik 5

CBCentralManager引用应作为类的成员变量强烈引用该类。它不能用作本地参考。

尝试下一个:

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)
   }
}
Run Code Online (Sandbox Code Playgroud)