如何从多个通知中识别每个 BLE 设备?

How do I identify each BLE device from multiple notification?

我想向多个设备写入相同的命令并从每个设备接收通知。使用我当前的代码,我可以做到这一点,但我无法告诉每个通知对应的 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);
                    }
                });
    }

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

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

谢谢。

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

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());