2016-11-06 42 views
0

我遇到了一個問題,即scanBleDevices(UUID ... filters)方法不支持使用不同的UUIDServices發現雙類型的設備。Rxandroidble-方法scanBleDevices(UUID ... filters)不支持兩種類型的服務

我猜args之間的關係是AND,但不是OR。但是我怎樣才能獲得具有不同UUIDService的雙重設備?

下面的代碼是我想用uuid 00001801-0000-1000-8000-00805F9B34FB和另一個帶uuid 6E400001-B5A3-F393-E0A9-E50E24DCCA9E的設備來發現設備,但我始終無法用代碼獲得結果。 那麼,我該如何解決這個問題呢?

scanScription = rxBleClient 
      .scanBleDevices(UUID.fromString("00001801-0000-1000-8000-00805F9B34FB"), UUID.fromString("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")) 
      .subscribe(new Action1<RxBleScanResult>() { 
     @Override 
     public void call(RxBleScanResult rxBleScanResult) { 
      if (!bleDeviceHashMap.containsKey(rxBleScanResult.getBleDevice().getMacAddress())) { 
       bleDeviceHashMap.put(rxBleScanResult.getBleDevice().getMacAddress(), rxBleScanResult.getBleDevice()); 
       HashMap<String, String> ble = new HashMap<String, String>(); 
       ble.put("name", rxBleScanResult.getBleDevice().getName()); 
       ble.put("address", rxBleScanResult.getBleDevice().getMacAddress()); 
       bleDevices.add(ble); 
       adapter.notifyDataSetChanged(); 
      } 
     } 
    }); 

回答

2

您可以執行自己的篩選:

final UUIDUtil uuidUtil = new UUIDUtil(); // an util class for parsing advertisement scan record byte[] into UUIDs (part of the RxAndroidBle library) 
scanSubscription = rxBleClient 
     .scanBleDevices() 
     .filter(rxBleScanResult -> { 
      final List<UUID> uuids = uuidUtil.extractUUIDs(rxBleScanResult.getScanRecord()); 
      return uuids.contains(firstUuid) || uuids.contains(secondUuid); 
     }) 
     .subscribe(
      ... 
     ); 

你也可以把它分成兩個流馬上:

final UUIDUtil uuidUtil = new UUIDUtil(); 
    final Observable<RxBleScanResult> sharedScanResultObservable = rxBleClient 
      .scanBleDevices() 
      .share(); // sharing the scan between two subscriptions 

    firstScanSubscription = sharedScanResultObservable 
      .filter(rxBleScanResult -> uuidUtil 
        .extractUUIDs(rxBleScanResult.getScanRecord()) 
        .contains(firstUuid)) // checking for the first UUID 
      .subscribe(
       // reacting for the first type of devices   
      ); 

    secondScanSubscription = sharedScanResultObservable 
      .filter(rxBleScanResult -> uuidUtil 
        .extractUUIDs(rxBleScanResult.getScanRecord()) 
        .contains(secondUuid)) // checking for the second UUID 
      .subscribe(
       // reacting for the second type of devices 
      ); 
+0

娃〜O操作。我知道了 !謝謝你的幫助!! – Botasky

相關問題