cordova-plugin-bluetoothle icon indicating copy to clipboard operation
cordova-plugin-bluetoothle copied to clipboard

Plugin doesn't scan devices when phone's screen is off

Open n1705771 opened this issue 4 years ago • 13 comments

Hi Sir,

I applied this plugin in my ionic 3 application. I enable the application working in background mode. The plungin's scan method works well when application is in both foreground and background. But when phone's screen turns off, it will stop scanning peripheral devices. Could you please tell how to let plugin works even when phone's screen turns off?

Many thanks

n1705771 avatar Apr 26 '20 13:04 n1705771

The behavior may have changed, but checkout this part of the docs: https://github.com/randdusing/cordova-plugin-bluetoothle#background-modes-ios

randdusing avatar Apr 26 '20 18:04 randdusing

@randdusing Thanks

n1705771 avatar Apr 29 '20 04:04 n1705771

When my android phone's screen turns off, scanStart method only returns status: scanStarted and never return status: scanResult.

When phone's screen turns on, scanStart method can return status: scanResult whatever the app is in foreground or background.

Below is the parameters setting, how to make scanStart method returns status: scanResult even through the phone's screen turns off?

var params = {
     "services": [],
     "allowDuplicates": false,
     "scanMode": this.bluetoothle.SCAN_MODE_LOW_LATENCY,
     "matchMode": this.bluetoothle.MATCH_MODE_AGRESSIVE,
     "matchNum": this.bluetoothle.MATCH_NUM_MAX_ADVERTISEMENT,
     "callbackType": this.bluetoothle.CALLBACK_TYPE_ALL_MATCHES,
   }

n1705771 avatar Apr 29 '20 13:04 n1705771

I face same problem using Cordova with Android. Hope someone has a solution.

arnoudcommandeur avatar May 01 '20 22:05 arnoudcommandeur

Have you tried specifying a service UUID?

randdusing avatar May 01 '20 23:05 randdusing

Yes. I put UUID service in params like:

var scanParams = {
      "services": [
        'faa1'
      ],
      "allowDuplicates": false,
      "scanMode": this.bluetoothle.SCAN_MODE_LOW_LATENCY,
      "matchMode": this.bluetoothle.MATCH_MODE_AGRESSIVE,
      "matchNum": this.bluetoothle.MATCH_NUM_MAX_ADVERTISEMENT,
      "callbackType": this.bluetoothle.CALLBACK_TYPE_ALL_MATCHES,
    }

n1705771 avatar May 02 '20 06:05 n1705771

And I find the same behavior happen in advertising method too. When phone's screen turns off, application stop advertising too.

n1705771 avatar May 02 '20 06:05 n1705771

Maybe we should create a foreground service on Android?

arnoudcommandeur avatar May 02 '20 12:05 arnoudcommandeur

I increase the scan time, scan method works well now. I also increase the advertising time, but when screen turns off, advertising works not very well as scan.

Both scan and advertising run in one application. The timer I set for scan is scan for 15 sec and stop for 5 sec; the timer for advertising is advertising for 10 sec and stop for 5 sec.

Do you think this is a proper timeout setting? Thanks

n1705771 avatar May 03 '20 00:05 n1705771

@n1705771 I think the issue here is not really related to the plugin but the limitation of the browser itself. Since the screen is off (most likely will be the same as background state), everything is suspended. But once you bring the app to the foreground, everything works as intended. Is that the behaviour on your current app? I'm also thinking on how to solve this issue.

mattkhaw avatar May 21 '20 02:05 mattkhaw

~~This should be fixed thanks to @mattkhaw's PR: https://github.com/randdusing/cordova-plugin-bluetoothle/pull/607~~

randdusing avatar May 21 '20 04:05 randdusing

@randdusing Oh really? That fixes this? Maybe I should do more testing on this.

mattkhaw avatar May 21 '20 05:05 mattkhaw

Oops, I posted this on the wrong issue. This fixes #302

randdusing avatar May 21 '20 12:05 randdusing