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 连接无效”
我确实尝试添加NS蓝牙外设使用说明进入我的 info.plist 文件,但这不起作用。
但奇怪的是,如果我初始化CB中央经理直接而不是使用类那么一切都会正常工作。
仅当我尝试初始化时才会出现此问题CB中央经理通过在类的对象上创建BLE或任何其他与此相关的课程。
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)
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)