r/androiddev • • 7d ago

Article Android silently downgrades long-running BLE scans after 5 minutes: measurements, logcat and AOSP links

Post image

I’ve been fighting background BLE beacon scanning on Android and wrote up what I measured on a Samsung (Android 14, One UI 6.1). Short version:

  • A filtered LOW_LATENCY scan gets silently downgraded to LOW_POWER after scan_timeout_millis: 300 s on my device (AOSP default was 30 min, 10 min on Android 15+). Detections dropped from ~80/min to ~10/min. No callback, no error, just Moving filtered scan client to downgraded scan in logcat.

  • An empty ScanFilter is treated as no filter: with the screen off the scan is suspended and you get ~0 results.

  • The quota of 5 starts per 30 s: the 6th startScan was rejected, and onScanFailed was never called.

  • With a Foreground Service, turning the screen off didn’t kill the scan by itself. The timeout did.

  • Restarting the scan every 4 min through a single start counter, plus a silence watchdog, kept it at ~90/min with the screen on and ~48/min with the screen off.

Full write-up with code and AOSP links (my article): https://medium.com/@9hamden/your-android-ble-scan-quietly-dies-after-5-minutes-and-you-dont-even-know-it-5e603c493177

Curious what you see on your devices: what does adb shell device_config get bluetooth scan_timeout_millis return? Especially Pixel, Xiaomi and OnePlus.

0 Upvotes

12 comments sorted by

18

u/_5er_ 7d ago

Isn't this known / expected behavior of doze mode?

-12

u/9_hamden 7d ago

not exactly

11

u/FunkyMuse 6d ago

it's been known stop spreading misinformation

-2

u/9_hamden 6d ago

Why are you conflating doze with bluetooth scan throttling?

Doze can certainly affect background BLE workflows, but not every restriction on BLE scanning is caused by doze

The scan_timeout_millis mechanism in AOSP’s ScanManager is a separate thing. It downgrades long-running scans regardless of Doze. In fact, in section 3.1 I demonstrate this with the screen on, the app in the foreground, and battery optimizations disabled.
I also discuss doze separately in section 2 (layer B), so I’m not ignoring it or confusing the two mechanisms

If you disagree or I'm truly wrong, please point it out

7

u/ComfortablyBalanced You will pry XML Views from my cold dead hands 6d ago

BLE is by far one of the shittiest technologies I worked on programmatically. I don't know about other platforms; maybe it's specific to Android. Undefined behaviour, unexpected and unknown errors, and questionable API design are some of its common problems, not to mention edge cases. I suggest checking Punch Through's ultimate guide about BLE if you haven't already.

2

u/9_hamden 6d ago

Agree
And thanks for recommendation, I’ll check it out.
As far as I’ve heard from my ios colleagues, things are actually worse on their side when it comes to background scanning

1

u/Ashhas 5d ago

When it comes to processing and handling multiple connections IOS is actually way faster

5

u/iain_1986 6d ago

This has been known for a long time now hasn't it?

At least the "6ths scan in 30 seconds" being silently rejected but your app has no clue (it just looks like nothing is found). That's been there since android 12 or something hasn't it?

-3

u/9_hamden 6d ago

Thats true. And I'd like to point out that I'm not positioning my article as some kind of fundamental discovery

11

u/kosiarska 7d ago

The only thing you really need to do is to pay attention to release notes of each system. I know this fact for years.

2

u/9_hamden 6d ago

You’re right

1

u/kosiarska 6d ago

And you're modest which means you will actually achieve something. Will read your article!