程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Corebluetooth 在后台无法重新连接到 BLE 设备大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Corebluetooth 在后台无法重新连接到 BLE 设备?

开发过程中遇到Corebluetooth 在后台无法重新连接到 BLE 设备的问题如何解决?下面主要结合日常开发的经验,给出你关于Corebluetooth 在后台无法重新连接到 BLE 设备的解决方法建议,希望对你解决Corebluetooth 在后台无法重新连接到 BLE 设备有所启发或帮助;

我正在尝试在后台模式下使用 corebluetooth 连接到 BLE 设备。我已经有后台模式工作,目前在后台接收断开连接回调。

我像这样创建经理

centralManager = CBCentralManager(delegate: self,queue: nil,options: [CBCentralManagerOptionRestorEIDentifIErKey: "myCentralManagerIDentifIEr"])

然后我用

执行扫描
centralManager.scanForperipherals(withservices: nil,options: [CBCentralManagerScanoptionAllowDuplicatesKey: true])

然后我在扫描后连接到设备

func centralManager(_ central: CBCentralManager,dIDdiscover peripheral: CBPeripheral,advertisementData: [String: Any],RSSi RSSI: NSnumber) {

        if peripheral.name == "My BLE" {
            self.peripheral = peripheral
            self.peripheral.delegate = self
            centralManager.stopScan()
            // Connect!
            centralManager.connect(self.peripheral,options: nil)
        }
    }

现在当应用程序进入后台,并且 BLE 断开连接时,我收到了这个回调(这很好,它表明后台模式和状态恢复正在工作)

    func centralManager(_ central: CBCentralManager,dIDdisconnectPeripheral peripheral: CBPeripheral,error: Error?)
        {
            print("disconnected")
            peripheral.delegate = self
            centralManager.connect(peripheral,options: nil)
        }

我立即尝试重新连接。但即使设备重新上线,我也从未收到任何回电。 我的印象是,这个 API 的工作方式是操作系统会在我的应用程序处于后台时为我尝试连接,当 BLE 重新上线时,我会收到通知?

我有以下委托方法让我知道它何时连接

func centralManager(_ central: CBCentralManager,dIDConnect peripheral: CBPeripheral) {
        //send some user notifications and logs
        alert()
    }

dIDConnect 在第一次连接时被调用,但当 BLE 设备在后台重新上线时不会被第二次调用

据苹果documentation

当用户离开家时,iOS 设备可能最终会出现故障 锁的范围,导致与锁的连接丢失。在 此时,应用程序可以简单地调用 connectPeripheral:options: CBCentralManager 类的方法,并且因为连接请求 不要超时,iOS 设备会在用户返回时重新连接 家。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的Corebluetooth 在后台无法重新连接到 BLE 设备全部内容,希望文章能够帮你解决Corebluetooth 在后台无法重新连接到 BLE 设备所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:设备