-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Foreground Service on Android #643
Comments
I had the same problem for a while and your solution worked well :) The capawesome plugin works like a charm - why did you suggest it's not the right solution ? |
Hi! Do you have a working example that works together with with the community-bluetooth le plugin? The startForegroundService() function parameters from their interface are only with regards to (local) notifications. So I thought this would not work with the whole BLE plugin. Or do you use the moveToForeground() function? BR |
so turns out a foreground service (while it may work) is not the right approach for BLE background scanning a foreground service should not be running at all times, the documentation actually highlights more appropriate alternative, which is using a Pending Intent as a scan result and using a receiver that is assigned to the application that will make the OS launch the receiver even if the app was previously terminated see more here https://developer.android.com/develop/connectivity/bluetooth/ble/background I'll be opening a PR soon that handles the pending intent and receiver |
see #700 |
That's great thanks a lot! Will this then also hold a connection active to the BLE client, which was established when the app was in foreground and went to background? |
Depends what you're trying to do See the docs for further information on how to handle background activities They suggest subscribing to characteristic notifications if you're looking to get information from the device |
Yes thats exactly what I do with the plugin in my app:
And this part should kept running when the app is going to background. I had a look into the docs. I am familiar with Java a bit, but did not develop in native android. Would be awesome if the plugin has an option to put the notifications on a forground service. |
Hi @omercnet Unfortunately, the pending intent is not sufficient to ensure reliable background scanning occurs on Android. In fact, using a pending intent is no better than the existing callback-based scan here. The ProblemTo explain a bit more: A capacitor app is a Process that runs an Activity which contains the web browser used to execute your application's javascript code. That is, in order for your javascript to be executed, android needs to run a Process -> launch an Activity -> render your code in the web browser. The Activity part here is important, because as noted for the In the background section in Android's docs, Android no longer allows Activities to be started from the background. The key difference between using the callback-based scan and the pending intent based scan is that the pending intent scan can relaunch your applications process if Android has terminated the process for power reasons or due to the user navigating too far away. But with a hybrid capacitor app, the Activity is not restarted and therefore your javascript won't be executed. The SolutionUnfortunately, there is no easy answer here.
Side-note 1: The 5 minute pauseThere is another likely issue you will see that can "appear" like the scan is stopping. 5 minutes after the phone screen is turned off, an power optimization in the webview kicks in, and pauses all javascript processing (among other things). To ensure your javascript code continues operating beyond this 5min mark after the screen is off (or the app is put into the background), you need to use a plugin such as capacitor-background-mode, specifically the disableWebViewOptimizations() feature. Please note however, that this can dramatically increase the power usage of your app if you have any kinds of animations or frequent javascript executions. Side-note 2: The 40-50 minute pauseRoughly 40-50 minutes after the phone screen is turned off (assuming the phone is unplugged), the phone usually enters a deep doze mode. This generally pauses all javascript execution. Some phone manufacturers will also kill some active processes at this point. See dontkillmyapp for details about these issues. It can help sometimes to disable battery optimizations for your application to continue operating beyond this point. However... at some point, the phone will still go to sleep. Or... if your app uses too much power, Android will choose to kill it in order to conserve power. |
Is your feature request related to a problem? Please describe.
On Android the communication is interrupted after a few minutes Minutes when the app (Ionic/React) is in background. This comes from the Android OS power saving. On iOS this is not the case when BLE service has the permission to run in background. On Android the BLE service needs to be moved to a foreground service, otherwise it gets interrupted. For applications which need to stay connected in background to acquire sensor data for example or want to do a reconnect if the ble device gets lost, this is not currently not possible.
When the app comes from background, it seems that all the messages are spit out at one time, overloading nearly my backend. So it seems, that somehow it is running in BG, but the callback is not thrown.
Describe the solution you'd like
It would be great, if there would be a function to put at least the callback of the startNotifications() service into foreground, that we can still send and receive ble packets to the client. This would be also great for the scanning function as well as connect and disconnect if the connection of the ddevice was lost and to be able to do a reconnect in background.
Describe alternatives you've considered
I was looking into the https://github.com/capawesome-team/capacitor-plugins/tree/main/packages/android-foreground-service
plugin, but it seems it is not suited to do the task.
Additional context
Here it went into "standby mode":
Here it comes out of the background:
The text was updated successfully, but these errors were encountered: