[英]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. 使用我当前的代码,我可以执行此操作,但是我无法告诉每个通知对应的MAC地址,因为它们都共享相同的特征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. 使用bleDevice.getMacAddress()
获取MAC地址并不准确,因为与循环相比,响应通知要慢得多。 I always get the last bleDevice
in the loop. 我总是得到循环中的最后一个bleDevice
。
Hope there is some way to pass the MAC address over. 希望有某种方法可以传递MAC地址。
Thank you. 谢谢。
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.