The first application that I'm developing in Android involves BLE scanning in background. The application has two buttons to start the service (which makes the scan and logs the results) and to stop the service.
From the documentation I learned the differences between Service and IntentService. I suspected that using an IntentService wouldn't be the best approach since the Android system destroys the service when all the intents delivered to it are handled and therefore the scanning running in the work thread created by the service would also stop working. However, I wanted to give it a try to test my knowledge.
As I was expecting, the service is almost immediately destroyed after it is created. However, the logs informing about new devices detected keep on appearing, which means that the scanning process keeps working even when the service is destroyed. When I close the main activity the scan finally stops. I want to know why the scan process seems to be bound to the thread of the main activity and not to the thread (automatically) created by the intent service. Why the scan process keeps working even when the service has been destroyed?
I have found some SO answers related with services and BLE scanning (here and here), but no one addressing this specific topic. Could you give me some help?