katzer / cordova-plugin-background-mode

Keep app running in background
Apache License 2.0
1.38k stars 1.02k forks source link

Can't disconnect BLE devices when killing the app with background plugin on #261

Open Albertazzi88 opened 7 years ago

Albertazzi88 commented 7 years ago

I'm developing an app that interfaces with BLE devices. Inside the app I can pair the devices and monitor their status. If I close the app I want to disconnect the devices from the smartphone, and when I open it again, I want the app to reconnect automatically to known devices.

Everything works fine without using the background plugin, but I need to use it since the app needs to monitor the BLE devices even when it's in background.

The problem happens ONLY if I use the background plugin: if I open again the app, the known devices are not reconnected INSIDE the app, but it appears they are already/still connected to the smartphone from the previous time (even if not shown in the bluetooth devices in the phone settings). It seems that the bluetooth connection to known devices isn't really being closed when I kill the app. It appears like the device is still connected as therefore it doesn't show up when I scan in the app.

I can't reconnect until I close the app, switch off and then on again bluetooth, and reopen the app.

The steps are very simples:

1) I open and connect the first time from the phone to the BLE product 2) Communication is OK, data is sent and received 3) I close (kill) the application from the smartphone (Without turning off Bluetooth/BLE from smartphone) 4) I relaunch the application ---> Known device is not reconnected, but appears to be somehow still paired with the phone (if I press the pairing button it does not go into pairing mode, that happens if it is already bonded to a phone).

Why this behaviour? Looks like something in the plugin prevents the bluetooth connection to be closed on app closing. I specify that:

Thanks

Albertazzi88 commented 7 years ago

After some testings, it appears that it is an issue caused by the Foregroundservice which doesn't close when I close the app. I found out that forcing the closure of the app in the settings solve the problem, and when I open again the app everything works. I believe then that it is a problem of the service created by the background plugin, which is somehow not stopped when I swipe away the app.

I tried to add the stopWithTask = "true" in the Android Manifest in the service field, but this doesn't force the service to stop when I swipe away the app! How can this happen? Do you have any suggestion about that, or things I must be aware of when trying to close a service together with the app? I also tried to add "onTaksRemoved" and call "stopService" inside, so that the service should be stopped when the activity is closed, but this does not work too.

Any idea why it behaves like this? How can I force the service created by this plugin to stop?

Can it be that the service is not "binded" to the app, and therefore it doesn't see the stopWithTask when I close the app?

DavidWiesner commented 7 years ago

This is maybe related to issue #246 and #262 try to change. The problem is this plugin set the launchMode singleInstance to the cordova activity. You can try to use my fork meanwhile, where I have removed this launchMode

$ cordova plugin rm cordova-plugin-background-mode
$ cordova plugin add "https://github.com/DavidWiesner/cordova-plugin-background-mode#disable-singleInstance" 

Note that the double quotes are important.

katzer commented 7 years ago

@Albertazzi88 Thanks for your investigation. As I've only Samsung - did you manage to reproduce the issue with Samsung? I've hear about quite often that the service is still running however I am not able to reproduce that. Can you see in the logs that the service is still running or is the notification still present in the notification center?

Albertazzi88 commented 7 years ago

@katzer yes, I've noticed it also on a Samsung S3 Neo and S5 Neo, they both have this problem. From what I see, the notification is not present any longer in the notification center, but the problem persist.

The only way now is to make the app "crach" when I swipe it out using stopWithTask = "true" (don't know why it crashes anyway!), in this way everything is closed but an error message appear after the app is swiped out. This is certainly not the best way to act.

netmikey commented 7 years ago

I'm also seeing this in a similar fashion on a ZTE phone with Android 6.0: I'm writing an app that plays audio and should continue playing in the background. I enable background mode when the user starts playing audio and disable it when the audio stops playing.

If I kill the app from within the task list while my audio plays (hence: while the background mode is enabled), its GUI completely disappears, nothing indicates that the app is still running, but its audio keeps playing. The only way I found to get it to stop is to restart the app and kill it again while background mode is disabled (it somehow seems to collect up the running player that went rogue...).

dimitriscsd commented 7 years ago

I am facing exactly the same issue here. Oneplus 3T Android 7.1.1

Plugin version 0.7.2

I am connecting to a BLE device and when I swipe the app away, the connection is still on for some reason. Next time I start the app, I am not able to connect to the BLE device because it's in a 'stuck' state.

As mentioned before, if I do not call .enable() then swiping away does terminate the BLE connection properly.

A solution to this would be greatly appreciated.

dimitriscsd commented 7 years ago

I should also note that DavidWiesner's pull request did not help. The problem persists. I even tried installing from his fork.

There has to be a way to kill the service once the app is swiped away!

Here are some logs from my device, AFTER I swiped the app away. It looks like it's still trying to run cordova stuff, even though the app has been closed:

07-04 14:04:46.807 21364 21364 W cr_AwContents: Application attempted to call on a destroyed WebView 07-04 14:04:46.807 21364 21364 W cr_AwContents: java.lang.Throwable 07-04 14:04:46.807 21364 21364 W cr_AwContents: at org.chromium.android_webview.AwContents.isDestroyed(AwContents.java:330) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at org.chromium.android_webview.AwContents.isDestroyedOrNoOperation(AwContents.java:303) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at org.chromium.android_webview.AwContents.evaluateJavaScript(AwContents.java:580) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at com.android.webview.chromium.WebViewChromium.evaluateJavaScript(WebViewChromium.java:193) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at android.webkit.WebView.evaluateJavascript(WebView.java:1037) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at org.apache.cordova.engine.SystemWebViewEngine.evaluateJavascript(SystemWebViewEngine.java:343) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at org.apache.cordova.NativeToJsMessageQueue$EvalBridgeMode$1.run(NativeToJsMessageQueue.java:368) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at android.app.Activity.runOnUiThread(Activity.java:5947) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at org.apache.cordova.NativeToJsMessageQueue$EvalBridgeMode.onNativeToJsMessageAvailable(NativeToJsMessageQueue.java:364) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at org.apache.cordova.NativeToJsMessageQueue.enqueueMessage(NativeToJsMessageQueue.java:249) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at org.apache.cordova.NativeToJsMessageQueue.addPluginResult(NativeToJsMessageQueue.java:238) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at org.apache.cordova.CordovaWebViewImpl.sendPluginResult(CordovaWebViewImpl.java:307) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at org.apache.cordova.CallbackContext.sendPluginResult(CallbackContext.java:61) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at com.megster.cordova.ble.central.BLECentralPlugin.onLeScan(BLECentralPlugin.java:536) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at android.bluetooth.BluetoothAdapter$2.onScanResult(BluetoothAdapter.java:2402) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper$1.run(BluetoothLeScanner.java:400) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at android.os.Handler.handleCallback(Handler.java:751) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at android.os.Handler.dispatchMessage(Handler.java:95) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at android.os.Looper.loop(Looper.java:154) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at android.app.ActivityThread.main(ActivityThread.java:6290) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at java.lang.reflect.Method.invoke(Native Method) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 07-04 14:04:46.807 21364 21364 W cr_AwContents: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

giowild commented 3 years ago

Hi all, since you are experts about BLE, ionic and Background, I have the following issue:

https://github.com/don/cordova-plugin-ble-central/issues/850

Any advise or pointer would be greatly appreciated, thanks a lot!