Search code examples
androidrxandroidble

How do I identify each BLE device from multiple notification?


I would like to write the same command to several devices and receive notification from each of them. With my current code, I am able to do this but I can't tell the MAC address corresponding to the each notification since they are all sharing the same Characteristics UUID.

Here is my code:

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

Getting the MAC address using bleDevice.getMacAddress() is not accurate because the response notification is much slower compared to the loop. I always get the last bleDevice in the loop.

Hope there is some way to pass the MAC address over.

Thank you.


Solution

  • The first thing that you could do is to change the line:

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

    to

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

    and then you could call on success:

    onNotificationReceived(bytes, bleDevice.getMacAddress());