0

As title,

For simplicity, if i want to execute twice read operation with different UUID :

(I know RxAndroidBle have provided a multiple read function)

Observable<RxBleConnection> ob = device.establishConnection(false);

ob.flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(CHAR_WIFI_SSID))
        .subscribe(
                characteristicValue -> {
                    //2. then read Successfully here !!!!!
                },
                throwable -> {
                }
        );

ob.flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(CHAR_WIFI_SECURITY_MODE))
        .subscribe(
                characteristicValue -> {     
                },
                throwable -> {
                    //1. I got BleAlreadyConnectedException error first !!!!
                }
        );

Why does second subscribe() get BleAlreadyConnectedException ?

==========update==========

i found the solution, if i modify

device.establishConnection(false) ==> device.establishConnection(false).compose(new ConnectionSharingAdapter())

ConnectionSharingAdapter will do something like this:

sourceObservable.replay(1).refCount();

keep the last one emitted by source observable

楊舜淼
  • 73
  • 6

1 Answers1

0

Subscribing twice to the same Observable will invoke subscription logic twice, which can be redundant at some cases or faulty like your case, where you are establishing multiple connections to Ble which is forbidden and got the BleAlreadyConnectedException.
as Dean Xu pointed out, you should multicast your Observable to prevent that. (you can use various publish/share operators)

yosriz
  • 10,147
  • 2
  • 24
  • 38
  • Maybe i have to change my question... beacuse i read different UUID ,there are two difference topic. it's not the case : the same topic and multiple observers – 楊舜淼 Jun 19 '17 at 07:38
  • not sure I understand your meaning, but the problem is the multiple `device.establishConnection`, you can read 2 different UUID , it just the connection itself that cannot be created twice, thus you need to multicast the connection, and then create 2 separate topics from the same connection. – yosriz Jun 19 '17 at 08:13
  • i just call device.establishConnection() once, so i don't know why i get this exception... – 楊舜淼 Jun 19 '17 at 09:27
  • 1
    seems you apply the solution at your edit to the answer, anyhow, `device.establishConnection()` just creates the Observable not performing the actual connect, when you subscribe the Observable subscription logic is executed, and as you subscribe twice it was executed twice – yosriz Jun 19 '17 at 10:32
  • Thanks for your explanation, I also do some test, ob = establishConnection(false).publish() and followed by ob.connect(), then call twice ob.flatMap().subscribe() ...., it's ok, no any error... – 楊舜淼 Jun 19 '17 at 10:52