【问题标题】:iOS Bluetooth connected according to Settings, but my app has no connected peripheralsiOS蓝牙根据设置连接,但我的应用没有连接外围设备
【发布时间】:2017-07-12 20:19:20
【问题描述】:

我正在为自定义蓝牙设备制作应用程序。该应用程序与设备正确连接/通信,但有时会出现一些奇怪的连接/重新连接问题。

此问题的一个可重现情况是电池耗尽时:

  1. 应用与 BT 设备配合良好
  2. BT 设备的电池没电了,与应用断开连接
  3. BT 设备电池已充电,尝试重新连接到应用
  4. BT 设备连接到操作系统(Apple 的 BT 设置显示设备已连接)
  5. App 发现所有 CBCharacteristics,并且该特征的 isNotifying 属性为 false
  6. 应用对所需特征执行 setNotifyValue 为 true
  7. 问题:这个函数从未被调用过:func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)
  8. 问题:self.manager.retrieveConnectedPeripherals(withServices: [identifier]) 返回未连接的外围设备,即使 Apple 的 BT 设置表明设备已连接。

这是第 8 步中管理器的启动方式:

fileprivate override init() {
    super.init()
    self.manager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey : Constants.cbCentralManagerOptionRestoreIdentifierKey])
}

问题:有人知道这可能是什么原因吗?或者解决方法是什么?

可能的线索:

  • 我需要在Apple的BT设置中选择忘记两次设备才能忘记设备(第一次忘记后,设备仍然连接并显示信息按钮)

  • 我启用了后台蓝牙

  • 我在调试器中收到以下消息:

[CoreBluetooth] XPC 连接无效

  • 我使用的是 Swift 3、Xcode 8

【问题讨论】:

  • 当连接丢失时,如何在 IOS 代码中启动连接?你应该在didDisconnect中调用peripheral.connect()
  • 要重新连接,当调用 centralManager didDisconnectPeripheral 时,我调用 self.manager.connect(peripheral, options: nil)(其中 self.manager 与上述步骤 8 相同)

标签: ios bluetooth core-bluetooth cbperipheral cbcentralmanager


【解决方案1】:

如果您的 BLE 设备断开连接,那么您需要调用此函数

[centralObj connectPeripheral:peripheral options:nil];

在委托方法中的 didDisconnectPeripheral 中。 您无需每次都调用 scanForPeripheralsWithServices

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2016-09-13
    • 2012-01-20
    相关资源
    最近更新 更多