Rxandroidble - 方法scanBleDevices(UUID ... filters)不支持两种类型的服务

时间:2016-11-06 08:13:37

标签: android rxandroidble

我遇到的问题是scanBleDevices(UUID ...过滤器)方法无法支持发现具有不同UUIDServices的双种设备。

我猜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();
            }
        }
    });

1 个答案:

答案 0 :(得分: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
            );