You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift iOS开发扫描BLE外设时持续出现[CoreBluetooth] XPC连接无效错误

解决CoreBluetooth XPC连接无效的问题

问题根源

你的代码中,BluetoothManager实例是viewDidLoad内的局部变量,当viewDidLoad执行完成后,该实例会被ARC自动回收。CBCentralManager的代理对象被销毁后,CoreBluetooth无法继续与你的代码通信,因此触发XPC connection invalid错误。

修复方案

在ViewController中将BluetoothManager声明为实例属性,延长其生命周期:

class ViewController: UIViewController {
    // 声明为实例属性,避免被提前释放
    private var bluetoothManager: BluetoothManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        bluetoothManager = BluetoothManager()
    }
}

额外检查项

  • 确认Info.plist的蓝牙权限配置完整:
    • iOS 13及以上需添加NSBluetoothAlwaysUsageDescription,iOS 12及以下需添加NSBluetoothPeripheralUsageDescription,且都要填写明确的权限用途说明,否则系统会拒绝蓝牙权限申请。
  • 验证测试设备蓝牙已开启,且App已获得蓝牙权限(可在系统设置中查看)。

内容的提问来源于stack exchange,提问作者AjayR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 09:30:43