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)
}
}
}
这是我的代码,每当我运行它时,它会给我一个消息:
"[CoreBluetooth] XPC连接无效"
我尝试将NSBluetoothPeripheralUsageDescription添加到我的info.plist文件中,但没有起作用。
奇怪的是,如果我直接初始化CBCentralManager而不使用类,则一切正常。
只有当我尝试通过创建BLE或任何其他类的对象来初始化CBCentralManager时,才会出现此问题。