1

I'm using Ionic Framework, Angular and Typescript to scan for BLE devices. I'm doing a scan every 3 minutes and waiting for 1 minute before each scan.

I'm using the Background Mode plugin, BLE plugin, PowerManagement plugin, Geolocation plugin from Ionic native.

The app works perfectly while scanning in Foreground mode.

When I try to scan in Background mode the app works only on some devices, it doesn't scan on Samsung, OnePlus, and some Redmi devices.

The BLE.startscan([]) is never called in Background mode. How can I fix that?

I don't see any errors using logcat.

I added the new permissions for Bluetoth - BLUETOOTH_SCAN, BLUETOOTH_CONNECT and BLUETOOTH_ADVERTISE

I'm stuck. I will be really grateful if someone can help me.

Thank you

Sergeeva
  • 31
  • 7
  • Could this just be an energy saving restriction from the OS? Maybe try changing the settings in Settings -> Apps -> -> battery to "not optimized" – Michael Kotzjan Feb 11 '22 at 08:58
  • the background mode plugin has an option where it disables the battery optimization this.backgroundMode.on('activate').subscribe(() => { this.backgroundMode.disableWebViewOptimizations(); this.backgroundMode.disableBatteryOptimizations(); console.log('background activated !!!!'); }); – Sergeeva Feb 11 '22 at 09:06
  • Maybe this option doesn't work on all devices. Have you compared the OS settings between the working and not working devices? – Michael Kotzjan Feb 11 '22 at 10:08
  • 1
    I don't think the problem comes from the energy saving restrictions. I change the battery optimization settings manually and I still can't scan in background I'm also using PowerOptimization plugin from Ionic to request for battery optimization – Sergeeva Feb 11 '22 at 10:17
  • Thanks for checking anyway! :) – Michael Kotzjan Feb 11 '22 at 11:40
  • Is it possible that the Background Mode plugin and the BLE plugin are incompatible? – Sergeeva Feb 11 '22 at 13:06
  • 1
    You need to use a foreground service, because "Android versions 8+ restrict services from running in the background to only 10 minutes after an app leaves the foreground. This blocks background beacon detections. By default on Android 8+, the library will use the JobScheduler to run scans, but these are limited to at most every 15 minutes. For use cases where apps need frequent beacon scanning in the background, a foreground service is a viable alternative." from [Android Beacon Library](https://altbeacon.github.io/android-beacon-library/foreground-service.html). ( – fabik111 Feb 11 '22 at 20:14
  • The problem was in the Background Mode plugin. It's not updated to work with the latest Android versions. Using this plugin -> https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background/src/master/ now I'm able to scan in background for BLE devices – Sergeeva Feb 22 '22 at 08:55

0 Answers0