Android Question Best practice - what to do with my Bluetooth Service when app isn't active?

Arf

Well-Known Member
Licensed User
Longtime User
I have a big service module which handles bluetooth connectivity with my remote device, all working very well now. Currently, the service remains running when I exit my app and this is what worries me - if another bluetooth device attempts to connect to the tablet, it will run into my service, with unpredictable results (most probably just results in bluetooth admin re-init).

I also seem to remember that if I enter the OS bluetooth settings and start a scan, my service also has an impact on that - although its possible that has changed and I have forgotten.

At any rate: I guess one thing to do is terminate my app and the service completely when my app is closed by the user. I understand that manually terminating apps and services is bad practice though.

Or, I could terminate my app and leave the service running, but only allow the service to interact with connecting devices when it detects that my apps activities are running. This may or may not be helpful.

Or thirdly I can just leave it as is and not worry too much about it.

Any guidance as to what I should or shouldn't try would be much appreciated.
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,
It is not dangerous at all to stop a service (if you manage the process globals correctly if any used or referenced in the service).

But for user experience it could be good to allow the user start and stop its action when the user doesn't want to have it working (or he/she could ask himself why your app could still run when not needed).

Another option could be to register the Bluetooth device your app often is used with, activate a service when that device connects and choose if something has to be started).

But in any case a service running in the background could be dangerous still you don't block or do something harmful you could decide or not have thought to).
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
OK thanks.
I am considering adopting the following approach then:

App is launched, the app starts the my bluetooth service. Or possibly device connection launches app which launches service. The activities maintain a flag in the service which let the service know if the app is active or not.
When the user closes the app, I set the flag in the service module to let it know we're done and let the app go into the background with a Activity.Finish. The service module then shuts down my remote bluetooth device, closes asynchsteam and serial1 and kills itself and any background activity with an ExitApplication.

Does that sound like the right way to do things?

The only thing I am not sure about, is my service obviously takes over handling of the bluetooth when it sparks into life. Should I record whether bluetooth is enabled or disabled when my activity starts, and ensure it is in the same state when I ExitApplication? I am not sure this is possible because bluetooth Admin would be created within my service, so if I use it to activate the bluetooth before exiting, will whatever was handling the bluetooth before my app was launched (OS I guess) be able to carry on regardless without issue? I guess this is what you meant by 'manage process globals correctly'.

Thanks
 
Upvote 0
Top