I've implemented a Service that connects to a Bluetooth device and obtains data from it. I need the Service to keep alive, receiving the data, when the app is in the background. I know that I need to implement a Foreground Service, and I know how to do it. My problem is that I don't want the service's notification to show up while the Service is not connected, or when it is not live measuring, or while the user has the app opened.
So, my question would be: Can I use startForeground(int, Notification) and stopForeground(boolean) within the service, in order to move it between foreground and background?
The docu says that Services started with startForegroundService() have 5 seconds to push the notification. Is the service killed if it doesn't push the notification?