0

我想将相同的命令写入多个设备并从每个设备接收通知。使用我当前的代码,我可以做到这一点,但我无法告诉每个通知对应的 MAC 地址,因为它们都共享相同的特性 UUID。

这是我的代码:

for (Map.Entry<String, RxBleDevice> entry : rxBleDeviceHashMap.entrySet()) {
        bleDevice = MyApplication.getRxBleClient(context).getBleDevice(entry.getKey()); // key is MAC
        connectionObservable = prepareConnectionObservable();
        connectionObservable
                .flatMap(new Func1<RxBleConnection, Observable<Observable<byte[]>>>() {
                             @Override
                             public Observable<Observable<byte[]>> call(RxBleConnection connection) {
                                 return connection.setupNotification(UUID.fromString(Constants.CHARACTERISTIC_UUID));
                             }
                         }, new Func2<RxBleConnection, Observable<byte[]>, Observable<byte[]>>() {
                             @Override
                             public Observable<byte[]> call(RxBleConnection connection, Observable<byte[]> apScanDataNotificationObservable) {
                                 return Observable.combineLatest(
                                         connection.writeCharacteristic(UUID.fromString(Constants.CHARACTERISTIC_UUID), command),
                                         apScanDataNotificationObservable.first(),
                                         new Func2<byte[], byte[], byte[]>() {
                                             @Override
                                             public byte[] call(byte[] writtenBytes, byte[] responseBytes) {
                                                 runOnUiThread(new Runnable() {
                                                     @Override
                                                     public void run() {
                                                         onWriteSuccess(writtenBytes);
                                                     }
                                                 });
                                                 Log.d(TAG, "responseBytes: " + ByteUtil.bytesToHex(responseBytes));
                                                 return responseBytes;
                                             }
                                         }
                                 );
                             }
                         }
                ).flatMap(new Func1<Observable<byte[]>, Observable<byte[]>>() {
            @Override
            public Observable<byte[]> call(Observable<byte[]> observable) {
                return observable;
            }
        })
                .first()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<byte[]>() {
                    @Override
                    public void call(byte[] bytes) {
                        onNotificationReceived(bytes);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        onNotificationSetupFailure(throwable);
                    }
                });
    }

使用获取 MAC 地址bleDevice.getMacAddress()并不准确,因为与循环相比,响应通知要慢得多。我总是得到最后一个bleDevice循环。

希望有一些方法可以传递 MAC 地址。

谢谢你。

4

1 回答 1

1

您可以做的第一件事是更改行:

bleDevice = MyApplication.getRxBleClient(context).getBleDevice(entry.getKey()); // key is MAC 

RxBleDevice bleDevice = MyApplication.getRxBleClient(context).getBleDevice(entry.getKey()); // key is MAC

然后你可以呼吁成功:

onNotificationReceived(bytes, bleDevice.getMacAddress());
于 2017-05-08T20:46:08.083 回答