katzer / cordova-plugin-background-mode

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

bluetooth low energy plugin doesn't work when screen turns off #507

Open n1705771 opened 4 years ago

n1705771 commented 4 years ago

Hi Sir,

I apply background plugin and bluetooth low energy plugin in my ionic 3 application. I enable the application working in background mode. The bluetooth plugin works well when application is in both foreground and background. But when phone's screen turns off, it will stop scanning peripheral devices. I have tried wakeUp() and unlock() methods, but doesn't help. I am not sure this is a issue of bakcground plugin or bluetooth low energy plugin. Could you please give some advice for this?

  if (this.globalvar.platform.is('android')){
  const options = {
    id: -1,
    title: 'Near',
    text: 'Running in Background Mode',
    hidden: false,
    silent: false,
    sticky: true,
    resume: true,
    foreground: true,
  };

  cordova.plugins.backgroundMode.setDefaults(options);

  cordova.plugins.backgroundMode.enable();

  cordova.plugins.backgroundMode.wakeUp();
  cordova.plugins.backgroundMode.unlock();

  cordova.plugins.backgroundMode.on('activate', () => {
    cordova.plugins.backgroundMode.disableWebViewOptimizations();
    console.log('background mode activate !!!');
  });

  cordova.plugins.backgroundMode.on('deactivate', () => {
    console.log("background deactivate xxxx ");
  });

} else {
  cordova.plugins.backgroundMode.enable();
}

Many thanks

helllamer commented 4 years ago

Please describe BLE scanning options.

Problem may relate to android-10 issues. For background scanning you should always define at least one ServiceUUID of target peripherals. For bg.scanning on iOS you also need to define some code in config.xml to allow it.

n1705771 commented 4 years ago

@helllamer Thanks. I use ionic native ble, I am not sure how to set BLE scanning options. I just called scan method as below code:

import { BLE } from '@ionic-native/ble';
...
public ble: BLE,
...
this.ble.scan([], 5).subscribe(
...
helllamer commented 4 years ago

Try something like this: this.ble.scan(['faa1'], 5)...

'faa1' - string, not number - an example ServiceUUID 0xFAA1.