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
我遇到了同样的问题:默认情况下沙箱是“打开”并禁用对蓝牙的访问。
对于运行 iOS 模拟器的人来说,蓝牙在其中不起作用。因此,如果您尝试使用蓝牙模拟应用程序,则会引发“[CoreBlueooth] XPC 连接无效”错误。
欲了解更多信息:https : //www.browserstack.com/test-on-ios-simulator
您需要在真实设备上测试您的应用。
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)
| 归档时间: |
|
| 查看次数: |
5325 次 |
| 最近记录: |