1

According to rxandroidble, dispose() should be called in onPause() of Activity lifeCycle, then the BLE connection will be close? And I can only connect BLE device in new Activity, and if I don't call dispose(), it will come up with BleAlreadyConnectedException(“Already connected to device with MAC address ***”) when I connect to BLE device in new Activity.

So, how can I maintain connection state between Activities?

Andy_Gu4y
  • 41
  • 3

1 Answers1

3

To keep a reference to anything longer than the lifecycle of an Activity one has to move the reference outside of the scope of this Activity.

On Android platform there are several ways to achieve this separation of lifecycles. By the book approach would be a Service which can be started by an Activity and stopped by a different one. Activities can communicate with the Service for instance by using binding — just be sure that the Service is started as it may be killed if left without any bound clients (activities).

Interface of the Service may vary on case-by-case basis — you will have to design what will suit your needs best.

Alternative (discouraged) approach could be the singleton pattern.

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21