Skip to content
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

Open
rainerfritz opened this issue Apr 9, 2024 · 7 comments
Open

Foreground Service on Android #643

rainerfritz opened this issue Apr 9, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@rainerfritz
Copy link

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":
Bildschirmfoto 2024-04-09 um 19 41 12

Here it comes out of the background:
Bildschirmfoto 2024-04-09 um 19 42 31

@rainerfritz rainerfritz added the enhancement New feature or request label Apr 9, 2024
@omercnet
Copy link

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 ?

@rainerfritz
Copy link
Author

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
Rainer

@omercnet
Copy link

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

@omercnet
Copy link

see #700

@rainerfritz
Copy link
Author

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?

@omercnet
Copy link

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

@rainerfritz
Copy link
Author

rainerfritz commented Oct 21, 2024

Yes thats exactly what I do with the plugin in my app:

await BleClient.startNotifications(
        devID,
        RAK_BLE_UART_SERVICE,
        RAK_BLE_UART_RXCHAR,
        (value) => {
          parseMsg(value).then(async (res) => {
            if (res !== undefined && 'msgTXT' in res) {
              // escape all quotation marks
              console.log("Connect: Text Message: " + res.msgTXT);
              await DatabaseService.writeTxtMsg(res);
              // do the notification if from another callsign
              const curr_call = ConfigObject.getConf().CALL;
              if (res.fromCall !== curr_call && (!res.msgTXT.startsWith("--")) && canNotify.current) {
                console.log("Connect: Notification from: " + res.fromCall);
                NotifyMsgState.update(s => {
                  s.notifyMsg = res;
                });
              }
            } 
            if (res !== undefined && 'temperature' in res) {
                console.log("Connect: Pos Msg from: " + res.callSign);
                await DatabaseService.writePos(res);
            }
            if (res !== undefined && 'mh_nodecall' in res) {
              console.log("Connect: Mheard Node Call: " + res.mh_nodecall);
              res.mh_nodecall = ConfigObject.getConf().CALL;
              MheardStaticStore.setMhArr(res);
            }

          })}).then(async () => {

        console.log('connected to device', devID);

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants