【发布时间】:2018-09-29 18:06:42
【问题描述】:
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)
}
}
}
这是我的代码,每当我运行它时,它都会给我消息
“[CoreBlueooth] XPC 连接无效”
我确实尝试将 NSBluetoothPeripheralUsageDescription 添加到我的 info.plist 文件中,但没有奏效。
但奇怪的是,如果我直接初始化 CBCentralManager 而不是使用类,那么一切正常。
仅当我尝试通过创建类 BLE 或任何其他类的对象来初始化 CBCentralManager 时才会出现此问题。
【问题讨论】:
-
你检查过这个网址吗? stackoverflow.com/questions/43880346/…
-
你必须在你的 info.plist 中添加一些键来解决这个问题
-
尝试在您的 appDelegate 中定义 CBCentralManager,在您的 BLE 类中使用相同的 CBCentralManager
-
在我问我的问题之前我已经遇到了那个 url,这就是为什么我首先尝试编辑我的 info.plist。也许您可以更具体地说明我应该添加哪些键?
-
我也尝试在我的 appDelegate 中定义 CBCentralManager,不幸的是没有运气
标签: swift core-bluetooth xpc