0

I'm currently login to my MQTT and subscribe to a topic inside onCreate() method of my activity. However it disconnects from MQTT as soon as I start another activity. How can I login/connect and subscribe to MQTT once and use the same connection in multiple activities of my app to publish?

hardillb
  • 54,545
  • 11
  • 67
  • 105

1 Answers1

2

Run the MQTT connection in a Android Service and make calls against the service to publish messages and use either broadcasts or callbacks to receive subscribed messages.

In fact you don't have to implement any of this as there is already a Paho Android client that will do it all for you.

https://eclipse.org/paho/clients/android/

hardillb
  • 54,545
  • 11
  • 67
  • 105