3

I faced the Android scan stopping issue in background when I am developing my COVID contact tracing app for my company. Here is what I have tried:

  1. Add foreground service
  2. Disable all the battery related optimization options in the phones
  3. Enable the application running in background
  4. Tesging devices:Galaxy S20 and Xperia with Android 10, Huawei with Android 8.

The scan stops immediately when going to background if you don't disable those battery optimization settings and application background update. After you disabled those settings, the scan can run about a couple of minutes(~5 minutes), then still stops. From the blog of David:http://www.davidgyoungtech.com/2017/08/07/beacon-detection-with-android-8, it seems that it is impossible to scan continuesly in background, because the JobScheduler will restart every 15 minutes while each scan lasts ~10 minutes at most. Is this the reality, or this is the best solution that I can scan 10 minutes at every 15 mins cycle?

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
Fred Guo
  • 31
  • 3
  • Could you use ADB to run a command like [this](https://stackoverflow.com/questions/65115840/is-it-possible-to-disable-wifi-throttling-on-androidxamarin-forms-if-the-devic/65151361#65151361)? Or you may refer to [this answer](https://stackoverflow.com/a/48691571/13329100). – zhangxaochen Dec 10 '20 at 07:24

1 Answers1

2

Background beacon detections are tricky to implement because many small issues can trip you up, and the specific issues vary by Android version, Android manufacturer and sometimes model. While Android 8+ restricts background ranging to every 15 mins using the Job Scheduler, if you add a Foreground Service, you can unlock unlimited background ranging.

A few tips:

  1. Focus first on the Galaxy S20 as Samsung behaviors are better documented and closer to vanilla Android. (Ideally you would test on a Pixel device first.) Only once it works on Samsung, move on to the others.

  2. Using the Android Beacon Library reference app configured with the built-in foreground service, I have seen detections of a standard iBeacon or AltBeacon continue on a Galaxy S10 indefinitely in the background, even with a 1.1 second scan period. See if you can reproduce the same.

  3. Be careful of Doze mode. If the CPU is put to sleep due to the phone being motionless with the screen off and not charging, your detections will stop. You can defeat Doze mode with wake locks, but it has a punishing impact on battery usage. You are better off accepting its limitations and keeping your phone in motion periodically during testing. If you want to get logs to see what is happening, use ADB commands to disable charging when connected via USB or learn to use ADB over WiFi.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Well, thanks David. That means it is possible to use foreground service to do continuous background scan, at least you have seen that in Galaxy S10. Despite the limitations of the manufacture, it means you can unlock the limitations of android level. For the Doze mode you mentioned, I did the similar thing as iOS - light the screen by notifications. Let me study your code and find out a way to achieve that. Will let you know. Appreciate your help. – Fred Guo Dec 11 '20 at 14:55
  • David, sorry that I have to ask the follow up question again: 1. The Android beacon library is normally for beacon detection, while my scenario is a little different that I need to do continuous background scanning, so I want some specific guide on that because it is painful for me to read those native code 3. All the posts I read direct me to two possible solutions:new startscan method using PendingIntent or Start Foreground service, but no one confirmed either one is the right solution. 4. I don't know if I tried the foreground service correctly, but it failed in my test. – Fred Guo Dec 16 '20 at 05:01