2

I'm writing some Bluetooth App. So I have written a class which handles the Bluetooth communication using threads and message handlers. At the moment I create an instance of this class in an Activity and this instance of my Bluetooth class establishes a connection. Fine.

Now when the display is rotated the Activity gets destroyed and so is my Bluetooth class instance, therefore it has to disconnect. I understand how to keep data through the display rotation and I already implemented that the recreated Bluetooth class instance will automatically reconnect after a rotation.

However I would like to keep the Bluetooth connection up during rotation, that is I not only want to preserve some data during the roation, I want to keep the Bluetooth class instance "alive". But to do so I would have to put the Bluetooth class instance to some save place out of the Activity.

It there any way to do this?

Shayan Ghosh
  • 882
  • 6
  • 14
  • 1
    Should the bluetooth connection be alive the hole application lifetime? You can create the connection in the Application class or create a singleton. – Zelldon Aug 19 '15 at 07:25
  • 1
    @Zelldon: Good hint. I didnt even know that an Application class exists. I will check that out! –  Aug 20 '15 at 08:38
  • So I tried it. What does work is that I can create a global (static) Bluetooth class instance which survives Activity cylces. But how can I shut down the global Bluetooth class instance (e.g. close the socket) when the App is terminated? I learned that onTerminate() is only for emulators, but does not work on real hardware. –  Aug 20 '15 at 13:22
  • Do you have a main activity? Which runs the hole time? So you can shutdown in the onDestroy - or use a counter and count how many activities are made if the last one is destroyed shutdown your bluetooth in on destroy. – Zelldon Aug 20 '15 at 13:28
  • Maybe it is a better way to use a service for the bluetooth like this http://stackoverflow.com/questions/15025852/how-to-move-bluetooth-activity-into-a-service – Zelldon Aug 20 '15 at 13:30
  • Thanks for the ideas. I guess a Service would be the best solution. I'll check it out. –  Aug 24 '15 at 06:01

0 Answers0