Is there a way to get a Subscription
from an already connected device using RxAndroidBle? I can't seem to find a way aside from disconnecting and connecting again.

- 3,212
- 2
- 14
- 21

- 494
- 6
- 23
-
if the user connects his device outside of the app (BT os menu for example), the connection isn't available – Daniele Oliva Apr 01 '20 at 13:36
2 Answers
You are probably not interested in getting the Subscription
from an already connected device but rather to get an instance of the emitted RxBleConnection
.
It is more of an RxJava
question then:
You can get this kind of behaviour (an Observable<RxBleConnection>
that will emit RxBleConnection
to any next subscriber if it had already emitted) for instance by using RxReplayingShare
. It would look like:
Observable<RxBleConnection> connectionObservable = rxBleDevice.establishConnection(false)
.compose(ReplayingShare.instance());
Then, every subscriber to the connectionObservable
will get the same RxBleConnection
as long as it will be valid.
Be careful though because sometimes BLE communication is implemented as stateful (i.e. a request-response model) and this kind of sharing may introduce some hard to spot issues.

- 19,192
- 5
- 36
- 77

- 3,212
- 2
- 14
- 21
No, you need to maintain the rxbleconnection
subscription and instance yourself. The library is stateless.

- 1,031
- 8
- 10