I connect my tablet with a bluetooth module of Chinese origin (it works like a master). When you rotate the screen, all threads are killed and new ones are created. If after turning the screen and before the line

socket.connect() 

do not delay, then the connection fails. With a delay of 100 ms, everything is connected. Question two:

  1. Am I doing right that I am killing an existing compound (maybe there is some way to keep it - in general, the streams remain alive after turning the screen)? Now I save the address of the remote device with the savedInstanceState and when I turn the screen I re-create the connection stream and the connection processing thread.
  2. If the first question is "yes", then what delay is better to put. In my opinion, 100 ms is a lot (I don't want to poke ...)

    2 answers 2

    No, not right. Killing the connection is not necessary. Create a thread that will belong to the process, not the activation, and let it work by sending out notifications. And if even more on the right - make the service :)

    • Pro flow belonging to the process can be more? Service, IMHO, from a gun on sparrows in my case ... - RomanoO
    • one
      The easiest way is to create a static field in your activity. But the main thing in this thread is not to pull the visual elements, and then there will be a drain on resources. - KoVadim
    • Not everything is visual in activites and fragments. Everything is done in my way ... Apparently, it is necessary to revise the work of the buttons that send control signals. - RomanoO
    • @KoVadim, thank you :) Works. Plus you - RomanoO

    It is not clear why to associate threads with the rotation of the screen. Rotate the screen should only affect the drawing of elements, but not on the logic of the program. I think you are very much into something with the architecture of the application. Take out work with bluetooth in a separate class / stream and let it work quietly there. According to Activity.onCreate() you need to display the current state of the connection and its progress, and it is not very reasonable to link the launch of the connection to the opening / closing of the Activity .

    It is better to "land" the start of the stream to Application.onCreate() - then it will always work regardless of the open / close Activity

    • The architecture repeats the one in the BluetoothChat example. Everything is as you wrote. So, apparently, it is necessary to work with the buttons that send control signals - RomanoO