【问题标题】:Can't connect to BLE Peripheral in some iOS devices在某些 iOS 设备中无法连接到 BLE 外设
【发布时间】:2021-08-07 18:51:20
【问题描述】:

BLE 在 7 Plus (iOS 14.4.2) 和 6 (iOS 12) 上运行良好。但是在 XR (14.4.2) 和 11 连接在centralManager.connect(peripheral, options: nil) 之后卡住(无限连接)

外围设备处于连接模式,因为其他智能手机无法检测到它。 一开始以为是外围设备本身的射频模块(NRF52)有问题,但是调试板也出现了问题。 重新启动智能手机没有帮助。 有趣的是,该应用程序在配备 M1 芯片的 MacBook 上运行良好

部分代码:

// Peripheral model

init(withPeripheral peripheral: CBPeripheral, advertisementData advertisementDictionary: [String : Any], andRSSI currentRSSI: NSNumber, using manager: CBCentralManager) {
    centralManager = manager
    basePeripheral = peripheral
    RSSI = currentRSSI
    super.init()
    advertisedName = parseAdvertisementData(advertisementDictionary)
    basePeripheral.delegate = self
}

public func connect() {
    centralManager.delegate = self
    centralManager.connect(basePeripheral, options: nil)
    print("Connecting to \(advertisedName ?? "device")...") 
// logs stops here
}

public func disconnect() {
    centralManager.cancelPeripheralConnection(basePeripheral)
    print("Cancelling connection with \(advertisedName ?? "device")...")
// triggers on VC dismiss
}

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    if central.state != .poweredOn {
        print("Central Manager stated changed to \(central.state)")
    }
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    if peripheral == basePeripheral {
        print("Connected to \(advertisedName ?? "device")")
        delegate?.peripheralDidConnect()
        discoverPrimaryServices()
    }
}

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
    if peripheral == basePeripheral {
        print("Disconnected from \(advertisedName ?? "device")")
        delegate?.peripheralDidDisconnect()
    }
}

【问题讨论】:

    标签: ios swift bluetooth-lowenergy core-bluetooth cbcentralmanager


    【解决方案1】:

    “外围设备处于连接模式,因为其他智能手机无法检测到它。”您的意思是其他智能手机可以检测到它吗?

    鉴于您列出的手机可以工作和不工作,我认为您的主板在蓝牙 5(iPhone 8 最初支持该功能)方面存在问题。 NRF52 支持 BT5(它支持 5.2),但如果您编写了自己的固件,您可能会破坏支持。我首先要确保你运行的是来自北欧的最普通的代码。

    【讨论】:

    • 我的意思是外围设备在连接到智能手机的阶段冻结时从扫描仪中消失,即它似乎正常工作而没有错误和断开连接,它根本无法在某些设备上完全连接。我基于 nRF Blinky 应用程序编写了我的代码,当然它进行了相当大的修改,但基础和一般模式保持不变。事实上,我替换了 UUID 服务和特性,并添加了许多我自己的。你说可能是板子本身固件的问题,我去那里找找问题,谢谢
    • 于是我们安装了NRF52(UART)的原始固件,在此基础上编写了我们的软件并且连接成功,显然问题确实出在外围设备方面。感谢您的提示!
    猜你喜欢
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 2022-12-19
    • 2013-10-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    相关资源
    最近更新 更多