2017-01-12 182 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的問題報告,因爲它可能需要花費一些時間來重現問題。 謝謝,