2017-01-12 146 views
0

我使用RxBluetoothKit 3.0.6,并有这样的代码在一个视图控制器:RxBluetoothkit调用retrievePeripherals失败,第二次

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呼叫失败 - 即运行程序时,首次显示此视图时,它可以正常工作。如果我打回来,立即重新打开视图,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连接无效”的消息与此或未连接。

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

回答

0

您能否告诉我self.manager是从外部注入的,还是可能在VC初始化时重新创建? 它究竟是如何创建的?

RxBluetoothKit什么是引擎盖下面简单地调用与上例中相同的方法,并将其映射到Observable序列。 你可以跳转到源代码和调试器,看看是否这行:每次调用 return self.centralManager.retrievePeripherals(withIdentifiers: identifiers)

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