Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift CoreBluetooth XPC连接无效_Swift_Core Bluetooth_Xpc - Fatal编程技术网

Swift CoreBluetooth XPC连接无效

Swift CoreBluetooth XPC连接无效,swift,core-bluetooth,xpc,Swift,Core Bluetooth,Xpc,这是我的代码,每当我运行它时,它都会给我消息 “[CoreBlueoth]XPC连接无效” 我确实尝试将NSBluetoothPeripheralUsageDescription添加到我的info.plist文件中,但没有成功 但奇怪的是,如果我直接初始化CBCentralManager而不是使用类,那么一切都可以正常工作 只有当我试图通过在类BLE或任何其他类的对象上创建来初始化CBCentralManager时,才会出现此问题。我遇到了相同的问题:默认情况下,沙箱是“打开”的,并且禁用对蓝牙

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

“[CoreBlueoth]XPC连接无效”

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

但奇怪的是,如果我直接初始化CBCentralManager而不是使用类,那么一切都可以正常工作


只有当我试图通过在类BLE或任何其他类的对象上创建来初始化CBCentralManager时,才会出现此问题。

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

确保您的目标功能允许蓝牙硬件访问(请参阅随附的屏幕截图)。

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

请尝试下一步:

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

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

有关进一步资料:


你需要在真正的设备上测试你的应用程序。

在我的情况下,结果是我打电话要求扫描太早了。我是从ViewDidLoad方法调用的。如果我通过按下按钮启动扫描,扫描会正常工作


我使用的解决方法是在从ViewDidLoad方法调用扫描时延迟使用performSelector。

我解决了在我的manager类中将NSObject更改为UIViewController的问题。

是否检查此url。你必须在你的info.plist中添加一些密钥才能解决这个问题。在你的appDelegate中定义CBCentralManager,在你的BLE Classis中使用相同的CBCentralManager。在我问我的问题之前,我已经遇到了这个url,这就是为什么我首先尝试编辑我的info.plist。也许你可以更具体地告诉我应该添加哪些键?我也尝试过在appDelegate中定义CBCentralManager,不幸的是我没有看到应用程序沙盒部分。查看背景说明这在XCode中不再有效。谢谢!在Xcode 11中为我工作-现在在“签名和功能”下
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)
   }
}