RxBluetoothkit调用retrievePeripherals第二次失败

时间:2017-01-12 09:04:06

标签: rx-swift rxiosble

我正在使用RxBluetoothKit 3.0.6并在视图控制器中使用此代码:

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    DDLogDebug("DEviceChooser viewDidLoad")
    self.navigationItem.rightBarButtonItem = self.editButtonItem
    //let timerQueue = DispatchQueue(label: "com.polidea.rxbluetoothkit.timer")
    //scheduler = ConcurrentDispatchQueueScheduler(queue: timerQueue)
    scheduler = MainScheduler.instance
    if let favs = EmaxProfile.getFavourites() {
        DDLogDebug("Got \(favs.count) favourites")
        var uuids: [UUID] = []
        for (s) in favs {
            if let uuid = UUID(uuidString: s) {
                uuids.append(uuid)
            }
        }
        DDLogDebug("Got \(uuids.count) uuids")
        if (!uuids.isEmpty) {
            let cbcm = CBCentralManager()
            let devs = cbcm.retrievePeripherals(withIdentifiers: uuids)
            DDLogDebug("Direct call to CBCentralManager.retrievePeripherals returned \(devs.count) entries")
            self.manager.retrievePeripherals(withIdentifiers: uuids)
                    .subscribeOn(MainScheduler.instance)
                    .subscribe(onNext: {
                        DDLogDebug("Got \($0.count) devices from RXBluetoothKit")
                        //var list = self.devices[self.favourites]
                        for (dev) in $0 {
                            DDLogDebug("Adding device \(dev.name)")
                            let btDev = BTDevice(dev)
                            btDev.suitable = true
                            self.devices[self.favourites].append(btDev)
                        }
                        if (self.devices[self.favourites].isEmpty) {
                            self.startScanning()
                        } else {
                            self.tableView.reloadData()
                        }
                    }, onCompleted: {
                        DDLogDebug("Retrieve complete")
                    }).addDisposableTo(disposeBag)
        } else {
            startScanning()
        }
    } else {
        startScanning()
    }
}

所以在viewDidLoad()我正在检索以前扫描过的设备列表,这些设备由UUIDS存储,并尝试在不扫描的情况下检索它们。如果我拨打核心蓝牙retrievePeripherals,它每次都能正常工作。但是,使用Rx BluetoothManager调用第二次失败 - 即当我运行程序时,第一次显示此视图时它可以正常工作。如果我点击Back然后立即重新打开视图,则onNext:闭包和onComplete:闭包都不会执行。日志输出为:

2017-01-12 19:55:13.824088 BlueMAX[559:216265] DEviceChooser viewDidLoad
2017-01-12 19:55:13.835820 BlueMAX[559:216297] Got 1 favourites
2017-01-12 19:55:13.836196 BlueMAX[559:216265] Got 1 uuids
2017-01-12 19:55:13.838832 BlueMAX[559:216265] Direct call to CBCentralManager.retrievePeripherals returned 1 entries
2017-01-12 19:55:13.846427 BlueMAX[559:216265] Got 1 devices from RXBluetoothKit
2017-01-12 19:55:13.846927 BlueMAX[559:216312] Adding device Optional("DEV")
2017-01-12 19:55:13.849145 BlueMAX[559:216265] Retrieve complete
2017-01-12 19:55:13.909986 BlueMAX[559:216312] [CoreBluetooth] XPC connection invalid
2017-01-12 19:55:21.515795 BlueMAX[559:216269] Saved 1 favourites, now exiting DeviceChooser
2017-01-12 19:55:22.054481 BlueMAX[559:216335] [CoreBluetooth] XPC connection invalid
2017-01-12 19:55:24.650717 BlueMAX[559:216269] DEviceChooser viewDidLoad
2017-01-12 19:55:24.651417 BlueMAX[559:216269] Got 1 favourites
2017-01-12 19:55:24.651646 BlueMAX[559:216312] Got 1 uuids
2017-01-12 19:55:24.654465 BlueMAX[559:216335] Direct call to CBCentralManager.retrievePeripherals returned 1 entries
2017-01-12 19:55:24.679889 BlueMAX[559:216269] [CoreBluetooth] XPC connection invalid

我不确定“XPC连接无效”消息是否与此相关。

这是一个错误,还是我做错了什么?

1 个答案:

答案 0 :(得分:0)

你能否告诉我self.manager是从外部注入,还是在VC的init中重新创建? 究竟是如何创建的?

RxBluetoothKit做什么只是在上面的示例中调用与您相同的方法并将其映射到Observable序列。 你能跳到源代码和调试器,看看这行是否: return self.centralManager.retrievePeripherals(withIdentifiers: identifiers) 每次都被召唤?

也许是为了方便 - 请将此报告为Github的问题,因为它可能需要花一些时间来重现问题。 谢谢,