<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Denis Khamidullin</title>
    <description>The latest articles on DEV Community by Denis Khamidullin (@nedmah).</description>
    <link>https://dev.to/nedmah</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4245929%2F237cd8b4-0d5d-4eab-ac1c-d07d38988db1.JPG</url>
      <title>DEV Community: Denis Khamidullin</title>
      <link>https://dev.to/nedmah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nedmah"/>
    <language>en</language>
    <item>
      <title>Your Android BLE scan quietly dies after 5 minutes, and you don't even know it</title>
      <dc:creator>Denis Khamidullin</dc:creator>
      <pubDate>Sun, 15 Nov 2026 12:51:23 +0000</pubDate>
      <link>https://dev.to/nedmah/your-android-ble-scan-quietly-dies-after-5-minutes-and-you-dont-even-know-it-1l60</link>
      <guid>https://dev.to/nedmah/your-android-ble-scan-quietly-dies-after-5-minutes-and-you-dont-even-know-it-1l60</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"What is essential is invisible to the eye."&lt;/em&gt;&lt;br&gt;&lt;br&gt;
Antoine de Saint-Exupéry, &lt;em&gt;The Little Prince&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hi everyone! I'm Denis Khamidullin, an Android developer at &lt;a href="https://navigine.com/" rel="noopener noreferrer"&gt;Navigine&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For more than ten years we have been building indoor positioning solutions for people, vehicles and equipment inside buildings and on enclosed sites, in industry, logistics and retail.&lt;/p&gt;

&lt;p&gt;In short, here is how our solution works. Positioning is based on Bluetooth Low Energy (BLE). The device scans BLE beacons: small autonomous devices that keep broadcasting short packets with an identifier (in iBeacon, that is UUID + major + minor) and the calibrated signal strength at 1 m. Measurements from several beacons are the input for algorithms that estimate the device position.&lt;/p&gt;

&lt;p&gt;More about our algorithms and technology is &lt;a href="https://navigine.com/blog" rel="noopener noreferrer"&gt;on our blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Quite often the tracked device is an ordinary Android phone in an employee's pocket. That means it has to work not only while unlocked, but also in the background with the screen off. This is where things get strange: on one phone everything is stable, on another the beacons "disappear" after half an hour, and on a third the app quietly stops working overnight.&lt;/p&gt;

&lt;p&gt;Most articles on this topic were written back in the Android 7–9 days and are badly outdated. In this article I will explain how BLE scanning works and why it degrades in the background on modern Android: which limits are built into the Bluetooth stack itself (with links to the AOSP sources) and what device manufacturers add on top. Then I will show how we solved the problem, which pitfalls we hit along the way, and what of this is confirmed by measurements on a real device.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faqytcic8ebbpehznznww.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faqytcic8ebbpehznznww.jpg" alt="stable_bg_meme" width="800" height="900"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Android keeps tightening the screws
&lt;/h2&gt;

&lt;p&gt;Android has been optimizing power consumption for years. It started with Android 6, which introduced Doze mode and App Standby. There are two reasons for this: saving battery and protecting user privacy. With every OS release it gets harder to do work in the background, and every app has to fight for its place in the sun.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Android version&lt;/th&gt;
&lt;th&gt;What changed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;6 (2015)&lt;/td&gt;
&lt;td&gt;Doze and App Standby; BLE scanning requires a runtime location permission&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Doze "on the go" (light doze); undocumented BLE limits: no more than 5 scan starts per 30 s, and a timeout for long-running scans&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8 / 8.1&lt;/td&gt;
&lt;td&gt;Background services are stopped a few minutes after the app goes to the background, so long-running work needs a Foreground Service; PendingIntent scanning appears; in 8.1, unfiltered scans are paused while the screen is off&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;App Standby Buckets: how often background work runs depends on how often the user opens the app&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;Separate &lt;code&gt;ACCESS_BACKGROUND_LOCATION&lt;/code&gt; permission; &lt;code&gt;foregroundServiceType="location"&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;"Allow all the time" location access can only be granted in Settings&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;Starting a Foreground Service from the background is banned (with exceptions); new &lt;code&gt;BLUETOOTH_SCAN&lt;/code&gt; permission&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;Bluetooth moves into a separate system module (Mainline); in QPR1 the 30-minute timeout also applies to filtered scans&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;Foreground Service type is mandatory, plus a separate permission for each type&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;Force stop cancels all of the app's PendingIntents, including a PendingIntent scan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;JobScheduler quotas apply even while a Foreground Service is running; the Bluetooth module is updated independently of the firmware&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;Per-app memory limits depending on the amount of RAM&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;On top of that, device manufacturers often ship their own Android skins that add their own background rules: "sleeping" apps, autostart controls, aggressive background cleanup. This has been a long-standing pain for every Android developer, but more on that later.&lt;/p&gt;

&lt;p&gt;All measurements in this article were taken on a Samsung device (Android 14, One UI 6.1). Data on other manufacturers comes from public sources.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The basic scan: how it works
&lt;/h2&gt;

&lt;p&gt;Let's start with the manifest, since that is where all permissions are declared. Every Android release asks for more of them. The set you need to scan beacons depends on the Android version:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Android&lt;/th&gt;
&lt;th&gt;What you need to scan beacons&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;6–9 (API 23–28)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;BLUETOOTH&lt;/code&gt;, &lt;code&gt;BLUETOOTH_ADMIN&lt;/code&gt; + runtime &lt;code&gt;ACCESS_COARSE_LOCATION&lt;/code&gt; or &lt;code&gt;ACCESS_FINE_LOCATION&lt;/code&gt;; location services turned on&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10–11 (API 29–30)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ACCESS_FINE_LOCATION&lt;/code&gt; (coarse is no longer enough); for background, &lt;code&gt;ACCESS_BACKGROUND_LOCATION&lt;/code&gt; (on 11, only via Settings)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12+ (API 31+)&lt;/td&gt;
&lt;td&gt;Runtime &lt;code&gt;BLUETOOTH_SCAN&lt;/code&gt; ("Nearby devices" group); beacons still need &lt;code&gt;ACCESS_FINE_LOCATION&lt;/code&gt;, and &lt;code&gt;ACCESS_BACKGROUND_LOCATION&lt;/code&gt; for background (see &lt;code&gt;neverForLocation&lt;/code&gt; below)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14+ (API 34+)&lt;/td&gt;
&lt;td&gt;For the background service: &lt;code&gt;FOREGROUND_SERVICE&lt;/code&gt; + &lt;code&gt;FOREGROUND_SERVICE_LOCATION&lt;/code&gt; and &lt;code&gt;foregroundServiceType="location"&lt;/code&gt;; such a service can be started from the background only with &lt;code&gt;ACCESS_BACKGROUND_LOCATION&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can already hit a couple of pitfalls at this stage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1) &lt;code&gt;neverForLocation&lt;/code&gt;.&lt;/strong&gt; The Android 12 documentation offers the &lt;code&gt;neverForLocation&lt;/code&gt; flag, which declares that you don't derive location from scan results. Tempting, but with this flag the system &lt;strong&gt;silently strips iBeacon and Eddystone packets from the results&lt;/strong&gt;. AOSP has a &lt;a href="https://android.googlesource.com/platform/packages/apps/Bluetooth/+/c9d72dcc6f61cf1354add29f25146f0bdef84baf" rel="noopener noreferrer"&gt;denylist&lt;/a&gt; for this, matching the signatures &lt;code&gt;0x004C 0x02&lt;/code&gt; (iBeacon) and &lt;code&gt;0xFEAA&lt;/code&gt; (Eddystone). In our case that means an empty screen without a single error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2) Checking legacy permissions.&lt;/strong&gt; If &lt;code&gt;BLUETOOTH&lt;/code&gt;/BLUETOOTH_ADMIN are declared with &lt;code&gt;maxSdkVersion="30"&lt;/code&gt; (as Google recommends), &lt;code&gt;checkSelfPermission&lt;/code&gt; returns DENIED for them on Android 12+. Code that checks them on every version will never start a scan.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.1 How to start a scan
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bluetoothManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;adapter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bluetoothLeScanner&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;ScanFilter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;// iBeacon: manufacturer Apple (0x004C), first two data bytes = 0x02 0x15&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setManufacturerData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x004C&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;byteArrayOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;byteArrayOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ScanSettings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setScanMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ScanSettings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SCAN_MODE_LOW_LATENCY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startScan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ScanCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onScanResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;callbackType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ScanResult&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* RSSI, scanRecord */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onScanFailed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errorCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* not always delivered */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ScanFilter&lt;/code&gt;&lt;/strong&gt; is what to look for: MAC, name, service UUID, service data, manufacturer data with a mask. Where possible, filters run right in the Bluetooth controller, so the CPU doesn't wake up for other people's devices. And as we'll see in section 3, without a filter background scanning simply doesn't work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ScanSettings&lt;/code&gt;&lt;/strong&gt; is how to look: the scan mode (&lt;code&gt;scanMode&lt;/code&gt;), the callback type (every packet / first match / match lost), and &lt;code&gt;reportDelay&lt;/code&gt; (buffer results in the controller and deliver them in batches).
### 1.2 What is a scan mode?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A beacon sends the same packet on three advertising channels (37, 38, 39) in turn, once per advertising interval (T_adv, usually 100–1000 ms). The phone doesn't listen to the air all the time either, but in windows: in each scan interval it listens on one channel for the length of the window and sleeps the rest of the time. The &lt;code&gt;SCAN_MODE_*&lt;/code&gt; constants are just names for window / interval pairs.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mode&lt;/th&gt;
&lt;th&gt;Window / interval, ms&lt;/th&gt;
&lt;th&gt;Share of time listening&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;LOW_POWER&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;140 / 1400&lt;/td&gt;
&lt;td&gt;10%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BALANCED&lt;/td&gt;
&lt;td&gt;183 / 730&lt;/td&gt;
&lt;td&gt;~25%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;LOW_LATENCY&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;100 / 100&lt;/td&gt;
&lt;td&gt;~100%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;SCREEN_OFF&lt;/code&gt; (internal)&lt;/td&gt;
&lt;td&gt;512 / 10 240&lt;/td&gt;
&lt;td&gt;5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;SCREEN_OFF_BALANCED&lt;/code&gt; (internal)&lt;/td&gt;
&lt;td&gt;183 / 730&lt;/td&gt;
&lt;td&gt;~25%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;We can't request the last two modes: they are &lt;a href="https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/1956456" rel="noopener noreferrer"&gt;internal to the system&lt;/a&gt;. More on that in section 3.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How many packets will we see?&lt;/strong&gt; If the window is longer than T_adv, a packet almost certainly lands in every window. If it is shorter, the chance to catch a packet in one window is roughly window / T_adv:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;detections per minute ≈ (60 000 / interval) × min(1, window / T_adv) × (1 − over-the-air loss)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our test beacon advertises every ~500 ms. In &lt;code&gt;LOW_LATENCY&lt;/code&gt; with the screen on, the phone saw 94 packets per minute out of ~120 possible: about 22% are lost even in ideal conditions. For &lt;code&gt;LOW_POWER&lt;/code&gt; the formula gives ~9–12 per minute, for BALANCED ~23–30.&lt;/p&gt;

&lt;p&gt;The takeaway: besides the rate, you need to watch the &lt;strong&gt;maximum gap&lt;/strong&gt; between detections. With a 10-second scan interval (&lt;code&gt;SCREEN_OFF&lt;/code&gt;), a person can walk 10–15 meters between two measurements.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.3 Where does the scan actually run?
&lt;/h3&gt;

&lt;p&gt;Call path: app → &lt;code&gt;BluetoothLeScanner&lt;/code&gt; → binder → Bluetooth system process (&lt;code&gt;GattService&lt;/code&gt; → &lt;code&gt;ScanManager&lt;/code&gt;) → native stack → HCI → Bluetooth controller.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7bz4rgt4x5oyzevw7d6p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7bz4rgt4x5oyzevw7d6p.png" alt="scan callpath" width="800" height="655"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This leads to three important points:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;There is one Bluetooth controller for the whole device.&lt;/strong&gt; All apps use it at once, and the system configures it. We can pass our wishes (mode, filters) and receive results in a callback. If another app is scanning in parallel with a more aggressive mode, the controller changes how it works. That is why the same app can behave differently on two identical phones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The system picks the final mode.&lt;/strong&gt; This is the job of &lt;code&gt;ScanManager&lt;/code&gt;, a component of the system Bluetooth service. It takes into account whether the screen is on, whether the app is visible to the user, and how long the scan has been running. Based on that, it can downgrade the mode we asked for. More on this in the next section.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bluetooth is a separate updatable module.&lt;/strong&gt; It has been a Mainline module since Android 13, and since Android 16 it is updated independently of the firmware. So scanning behavior depends both on the Android version and on the version of this module.
&lt;strong&gt;You have to babysit background scanning yourself.&lt;/strong&gt; For location, Android has ready-made system mechanisms such as Fused Location and geofences: the system collects data and wakes the app up. For long-running BLE scanning, unfortunately, there is no such mechanism. The app has to keep the scan alive on its own: run in a Foreground Service, restart the scan, and recover after the process dies.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  2. What breaks in the background, and why
&lt;/h2&gt;

&lt;p&gt;Background scanning problems fit neatly into three layers: the Bluetooth stack, the process lifecycle, and the manufacturer's firmware. Each layer can stop the scan on its own, regardless of the others.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsnqu6ah8xtlpwp2eprf5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsnqu6ah8xtlpwp2eprf5.png" alt="ble layers" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Layer A: the AOSP Bluetooth stack (ScanManager)
&lt;/h3&gt;

&lt;p&gt;This is what we described above: scan filters, windows and intervals, timeouts and limits. Think of it as the first line of defense. Most problems you run into while implementing scanning live right here. But beyond the trees there is a whole forest.&lt;/p&gt;

&lt;h3&gt;
  
  
  Layer B: the process lifecycle
&lt;/h3&gt;

&lt;p&gt;If you want (almost) guaranteed background scanning, you need a Foreground Service. Sounds simple: a couple of classes and you're done. In reality it is more involved, and depending on the OS version you have to follow a set of rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Android 12&lt;/strong&gt;: a Foreground Service can't be started from the background, with rare exceptions. Start the service while the app is on screen.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Android 14&lt;/strong&gt;: the service type is mandatory. For us that is &lt;code&gt;location&lt;/code&gt;, plus a separate &lt;code&gt;FOREGROUND_SERVICE_LOCATION&lt;/code&gt; permission; without location access the service won't start.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Android 15&lt;/strong&gt;: &lt;a href="https://developer.android.com/about/versions/15/behavior-changes-all#enhanced-stop-states" rel="noopener noreferrer"&gt;force stop cancels all of the app's PendingIntents&lt;/a&gt;, including a PendingIntent scan. &lt;code&gt;BOOT_COMPLETED&lt;/code&gt; receivers may no longer start some service types, but &lt;code&gt;location&lt;/code&gt; is not on that list.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Android 16&lt;/strong&gt;: &lt;a href="https://developer.android.com/about/versions/16/behavior-changes-all#job-quota-opt" rel="noopener noreferrer"&gt;JobScheduler quotas apply even while a Foreground Service is running&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Android 17&lt;/strong&gt;: per-app memory limits depending on the amount of RAM (&lt;a href="https://developer.android.com/about/versions/17/behavior-changes-all" rel="noopener noreferrer"&gt;behavior changes&lt;/a&gt;). When the limit is exceeded, the process is killed: &lt;code&gt;ApplicationExitInfo&lt;/code&gt; reports &lt;code&gt;REASON_OTHER&lt;/code&gt; with &lt;code&gt;MemoryLimiter:AnonSwap&lt;/code&gt; in the description. For now the limits are generous and aimed at memory leaks, but for a service that runs for days this matters.
WorkManager and JobScheduler won't do the job here, unfortunately: periodic work can run no more often than once every 15 minutes. They are still useful as a watchdog that restarts the scan with a PendingIntent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Three more things worth knowing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Doze.&lt;/strong&gt; When the phone lies still with the screen off and isn't charging, the system defers network access, alarms and background jobs. The scan itself runs in the Bluetooth process, but whatever you do with the results (uploading to a server, timers) falls under these restrictions. Some manufacturers have their own, harsher "deep" Doze. For example, on HyperOS developers &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/129338/how-does-nrf-connect-for-android-receive-ble-advertisements-while-the-phone-is-in-miui-deep-doze" rel="noopener noreferrer"&gt;report a log line&lt;/a&gt; where the system refuses to start a scan for non-system apps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Force stop.&lt;/strong&gt; After "Force stop" in Settings, the system tears everything down: the service, alarms, jobs, and since Android 15 the PendingIntent scan too. The app won't start again until the user opens it. This is a deliberate user action, and you shouldn't try to work around it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Process death.&lt;/strong&gt; When memory is low, the system can kill even a process with a Foreground Service. With &lt;code&gt;START_STICKY&lt;/code&gt; the service restarts by itself, but the scan has to be started again in &lt;code&gt;onStartCommand&lt;/code&gt; (our measurement is in section 3.5).
### Layer C: OEM firmware&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As mentioned earlier, manufacturers add their own power-saving mechanisms on top of Android, and these work below the API level. The service notification is showing, the code follows the documentation, and the firmware still decides the app doesn't need to run. The main restrictions from popular vendors:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Manufacturer&lt;/th&gt;
&lt;th&gt;What gets in the way&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Samsung (One UI)&lt;/td&gt;
&lt;td&gt;"Sleeping" and "deep sleeping" app lists: an app that hasn't been opened for several days is restricted automatically. Since One UI 6.0, Samsung states that Foreground Services of apps targeting Android 14 work as intended&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Xiaomi (MIUI, HyperOS)&lt;/td&gt;
&lt;td&gt;Autostart is off by default, and without it the service isn't restarted; aggressive background cleanup. On a Xiaomi 12T with the screen off, scan results arrive in a batch once every 5 minutes (&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/117280/android-problem-with-ble-scanning-stopped-while-screen-is-off-on-newer-phones" rel="noopener noreferrer"&gt;Nordic DevZone&lt;/a&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Huawei, Honor (EMUI, HarmonyOS)&lt;/td&gt;
&lt;td&gt;"App launch" in automatic mode blocks autostart and background activity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OPPO, Vivo, OnePlus&lt;/td&gt;
&lt;td&gt;Periodic cleanup of background processes, including ones with a Foreground Service&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There is a great site that tracks all of these restrictions; if you're an Android developer, you probably know it already: &lt;a href="https://dontkillmyapp.com/" rel="noopener noreferrer"&gt;dontkillmyapp.com&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. What we tested
&lt;/h2&gt;

&lt;p&gt;We ran a bare scanner on a Samsung test device (Android 14) under different conditions: screen on and off, app open and minimized, process killed, app swiped away from Recents. The scanner ran inside a Foreground Service of type &lt;code&gt;location&lt;/code&gt;, and the app was excluded from battery optimization.&lt;/p&gt;

&lt;p&gt;The short version: almost all problems come down to one mechanism, the long-running scan timeout. Below, in order: what we expected, what we saw, and how to check it yourself.&lt;/p&gt;

&lt;p&gt;A caveat: the app was excluded from battery optimization. Behavior on other manufacturers' devices may differ.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.1 The long-running scan timeout: 5 minutes
&lt;/h3&gt;

&lt;p&gt;We start &lt;code&gt;LOW_LATENCY&lt;/code&gt; with an iBeacon filter and leave the phone lying with the screen on. For the first five minutes everything is great: 75–100 detections per minute. In the sixth minute the rate drops to 5–14 per minute and never recovers. Having the app open on screen changes nothing: the drop is the same as in the background.&lt;/p&gt;

&lt;p&gt;The cause is right there in logcat:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;23:56:14.796 scan started (scannerId 7, LOW_LATENCY)
00:01:14.826 W BtGatt.ScanManager: Moving filtered scan client to downgraded scan (scannerId 7)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Exactly 300 seconds. This is an AOSP mechanism: if a scan runs longer than &lt;code&gt;scan_timeout_millis&lt;/code&gt;, &lt;code&gt;ScanManager&lt;/code&gt; moves it to &lt;code&gt;LOW_POWER&lt;/code&gt;, and an unfiltered scan to opportunistic mode. Downgrading filtered scans was &lt;a href="https://android.googlesource.com/platform/packages/modules/Bluetooth/+/6ef5ea6b439c0f27a0d2cf24a4226993025fd569" rel="noopener noreferrer"&gt;added in Android 13 QPR1&lt;/a&gt;. The app is told nothing: callbacks keep coming, just less often. In &lt;code&gt;dumpsys bluetooth_manager&lt;/code&gt; the scan gets a &lt;code&gt;Forced&lt;/code&gt; mark, while its settings still say &lt;code&gt;LOW_LATENCY&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In AOSP the &lt;a href="https://android.googlesource.com/platform/packages/apps/Bluetooth/+/26329f3f66bb33247a17a46efcc124db4dcfaf39" rel="noopener noreferrer"&gt;default is 30 minutes&lt;/a&gt;, but in practice the value varies from phone to phone. Starting with Android 15, Google lowered it to 10 minutes (600000 ms). You can check it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ adb shell device_config get bluetooth scan_timeout_millis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The good news: the timer is counted from each &lt;code&gt;startScan&lt;/code&gt; call. How we use that is in section 4.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.2 No filter vs. an empty filter
&lt;/h3&gt;

&lt;p&gt;Since Android 8.1, an unfiltered scan &lt;a href="https://android.googlesource.com/platform/packages/modules/Bluetooth/+/101f497021f1b6613f579ae66ff2b06a1859eba7" rel="noopener noreferrer"&gt;is paused when the screen turns off&lt;/a&gt;. The internet often suggests working around this with an empty filter, &lt;code&gt;ScanFilter.Builder().build()&lt;/code&gt;. That &lt;a href="https://android.googlesource.com/platform/packages/modules/Bluetooth/+/7f707ac3123f62e78cc090b934d3146326fc52bc" rel="noopener noreferrer"&gt;used to work&lt;/a&gt;. We tested three options:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Filter&lt;/th&gt;
&lt;th&gt;Screen on, detections/min&lt;/th&gt;
&lt;th&gt;Screen off, detections/min&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;No filter (&lt;code&gt;null&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;90&lt;/td&gt;
&lt;td&gt;~0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Empty &lt;code&gt;ScanFilter&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;80&lt;/td&gt;
&lt;td&gt;~0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;iBeacon (&lt;code&gt;0x004C&lt;/code&gt;, &lt;code&gt;02 15&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;80&lt;/td&gt;
&lt;td&gt;45–60&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The conclusion is simple: you need a real filter. For iBeacon, filter on Apple manufacturer data (&lt;code&gt;0x004C&lt;/code&gt;) with the &lt;code&gt;02 15&lt;/code&gt; prefix; you can also add your beacons' UUID.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.3 The start quota and silent rejection
&lt;/h3&gt;

&lt;p&gt;The system allows an app no more than 5 &lt;code&gt;startScan&lt;/code&gt; calls per 30 seconds (&lt;a href="https://android.googlesource.com/platform/packages/apps/Bluetooth/+/26329f3f66bb33247a17a46efcc124db4dcfaf39" rel="noopener noreferrer"&gt;&lt;code&gt;scan_quota_count&lt;/code&gt; and &lt;code&gt;scan_quota_window_millis&lt;/code&gt;&lt;/a&gt;). We restarted the scan 7 times about 5 seconds apart. The first five starts went through, and the system rejected the sixth:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;E BtGatt.GattService: App 'com.navigine.indoornavigationdemo' is scanning too frequently
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The seventh went through again: by then the first start had left the 30-second window.&lt;/p&gt;

&lt;p&gt;The worst part is that the app never learned about the rejection. Since API 33 there is an error code, &lt;code&gt;SCAN_FAILED_SCANNING_TOO_FREQUENTLY&lt;/code&gt;, but in our case &lt;code&gt;onScanFailed&lt;/code&gt; was never called. As far as the app knows, the scan is "running", yet there are no results. If your code has a "no results → restart" loop, it can easily run into the quota and switch itself off.&lt;/p&gt;

&lt;p&gt;So starts need their own counter, and &lt;code&gt;onScanFailed&lt;/code&gt; can't be your only signal.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.4 Screen off and a Foreground Service
&lt;/h3&gt;

&lt;p&gt;According to the &lt;a href="https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/1956456" rel="noopener noreferrer"&gt;AOSP code&lt;/a&gt;, a background app gets one of the power-saving &lt;code&gt;SCREEN_OFF&lt;/code&gt; modes when the screen is off. The exception is apps with a Foreground Service: they keep &lt;code&gt;LOW_LATENCY&lt;/code&gt;. Our Samsung confirmed this. We started a scan and turned the screen off after 2 minutes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Minute after start&lt;/th&gt;
&lt;th&gt;Screen&lt;/th&gt;
&lt;th&gt;Detections/min&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1–2&lt;/td&gt;
&lt;td&gt;on&lt;/td&gt;
&lt;td&gt;55, 80&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3–5&lt;/td&gt;
&lt;td&gt;off&lt;/td&gt;
&lt;td&gt;62, 45, 47&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6–7&lt;/td&gt;
&lt;td&gt;off&lt;/td&gt;
&lt;td&gt;17, 2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With the screen off there are about half as many detections, and &lt;code&gt;dumpsys&lt;/code&gt; shows why: the moment the screen turns off, Samsung switches &lt;code&gt;LOW_LATENCY&lt;/code&gt; from a 4096 ms window and 4096 ms interval (100%) to a 158.75 ms window and 305.6 ms interval, about 52% of the time.&lt;/p&gt;

&lt;p&gt;In the sixth minute, the same timeout from section 3.1 fires. So with a Foreground Service, turning the screen off doesn't cut the scan by itself. The timeout does, and with the screen off the downgrade &lt;a href="https://android.googlesource.com/platform/packages/modules/Bluetooth/+/2be67fd00171d8ced4fd39b86cadfff8d81a2348" rel="noopener noreferrer"&gt;can go even deeper&lt;/a&gt;. Restarting the scan solves this; more on that below.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.5 Process death and swiping from Recents
&lt;/h3&gt;

&lt;p&gt;We tested two scenarios.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Process killed (&lt;code&gt;kill -9&lt;/code&gt;).&lt;/strong&gt; The system restarted the &lt;code&gt;START_STICKY&lt;/code&gt; service after 1.7 seconds, and the first detection arrived 2.1 seconds after the kill. The new scan ran at full speed again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;App swiped away from Recents.&lt;/strong&gt; &lt;code&gt;onTaskRemoved&lt;/code&gt; arrived, but the process stayed alive: the service and the scan kept running, with a 0.3-second gap in the results stream. Without a running service, the same gesture kills the process on this phone (&lt;code&gt;ApplicationExitInfo&lt;/code&gt; reports &lt;code&gt;REMOVE TASK&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  4. How we solve it in our SDK
&lt;/h2&gt;

&lt;p&gt;Our scanning logic used to be fairly naive: &lt;code&gt;LOW_LATENCY&lt;/code&gt; while the app is on screen, &lt;code&gt;LOW_POWER&lt;/code&gt; with a filter by beacon manufacturers when it is minimized. The scan was started by app lifecycle events and then left to live on its own. In practice that meant everything described in section 3: after 5 minutes the detection rate dropped several times over, and after a process restart without UI the scan might not start at all.&lt;/p&gt;

&lt;p&gt;Through observation and trial and error, we reworked our logic to account for system limits as much as possible. Below, limit by limit: what the system does, what we do, pseudocode, and the cost of the solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  The long-running scan timeout
&lt;/h3&gt;

&lt;p&gt;As we found out, the scan is downgraded to &lt;code&gt;LOW_POWER&lt;/code&gt; &lt;code&gt;scan_timeout_millis&lt;/code&gt; after it starts. On our test device that is 5 minutes; the AOSP default used to be 30 (1800000 ms), and on Android 15+ devices it is 10 minutes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How we fixed it.&lt;/strong&gt; The system timer is counted from each &lt;code&gt;startScan&lt;/code&gt; call, so we restart the scan before it expires: once every 4 minutes. The app can't find out the exact timeout on a given phone, because it is &lt;a href="https://android.googlesource.com/platform/packages/modules/Bluetooth/+/c3ed86462631542cac8f9e838e509c4588326fd6" rel="noopener noreferrer"&gt;not allowed to read &lt;code&gt;device_config&lt;/code&gt;&lt;/a&gt;. So we pick an interval with a margin under the shortest timeout we know of.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="no"&gt;RESTART_PERIOD_MS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60_000L&lt;/span&gt; &lt;span class="c1"&gt;// shorter than the 5-minute timeout&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onScanStarted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeCallbacks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;restartTask&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postDelayed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;restartTask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;RESTART_PERIOD_MS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;restartTask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Runnable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;restart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// stopScan() + requestStart()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a test implementation of this approach, the drop at the fifth minute is gone:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;No restart, after minute 5&lt;/th&gt;
&lt;th&gt;Restart every 4 minutes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Screen on, detections/min&lt;/td&gt;
&lt;td&gt;~10&lt;/td&gt;
&lt;td&gt;92&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Screen off, detections/min&lt;/td&gt;
&lt;td&gt;2–6&lt;/td&gt;
&lt;td&gt;48&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxmplvchsrqtu1672p8hv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxmplvchsrqtu1672p8hv.png" alt="Beacon detections per minute with and without a scan restart" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The cost.&lt;/strong&gt; The radio spends more time in active mode, so battery drain is higher than with a downgraded scan. Each restart means a short gap in the stream (at most 3.4 s with the screen on in our test) and one start out of the quota, but once every 4 minutes this goes unnoticed. We did not measure battery drain separately.&lt;/p&gt;

&lt;h3&gt;
  
  
  No filter vs. an empty filter
&lt;/h3&gt;

&lt;p&gt;With the screen off, a scan with no filter or with an empty filter is paused.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How we fixed it.&lt;/strong&gt; We added filters for every beacon format our parser understands. Filters in the list are combined with OR: a packet passes if it matches at least one of them. Optionally, once a venue is loaded and its beacons' UUIDs are known, the iBeacon filter can be narrowed down to them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ScanFilter&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;// iBeacon: Apple (0x004C), prefix 02 15&lt;/span&gt;
    &lt;span class="nc"&gt;ScanFilter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setManufacturerData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x004C&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;byteArrayOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;byteArrayOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// iBecom&lt;/span&gt;
    &lt;span class="nc"&gt;ScanFilter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setManufacturerData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0059&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;byteArrayOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// Eddystone&lt;/span&gt;
    &lt;span class="nc"&gt;ScanFilter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setServiceData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ParcelUuid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0000FEAA-0000-1000-8000-00805F9B34FB"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;byteArrayOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// Kontakt.io&lt;/span&gt;
    &lt;span class="nc"&gt;ScanFilter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setServiceData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ParcelUuid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0000FE6A-0000-1000-8000-00805F9B34FB"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;byteArrayOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The cost.&lt;/strong&gt; Filters run in the controller, and the controller supports a limited number of them, so we keep the list short. A new beacon format without its own filter won't be visible in the background; each one has to be added explicitly.&lt;/p&gt;

&lt;h3&gt;
  
  
  The start quota and silent rejection
&lt;/h3&gt;

&lt;p&gt;The system doesn't allow more than 5 &lt;code&gt;startScan&lt;/code&gt; calls per 30 seconds and rejects the sixth.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How we fixed it.&lt;/strong&gt; Scan starts used to come from several places in the code, and not all of them respected the limit. Now every start (scheduled restart, mode change, watchdog, Bluetooth turned back on) goes through a single counter. If the limit is used up, the start is postponed until the oldest start leaves the 30-second window, plus a small margin. If more requests arrive in the meantime, only the latest one runs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StartQuota&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;windowMs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Long&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;starts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ArrayDeque&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;runWhenAllowed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SystemClock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;elapsedRealtime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;starts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;removeAll&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;windowMs&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;starts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;starts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addLast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;firstScheduling&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pending&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
            &lt;span class="n"&gt;pending&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="c1"&gt;// older requests are replaced by the latest one&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstScheduling&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;delay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;starts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;windowMs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2_000&lt;/span&gt;
                &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postDelayed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;pending&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;pending&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nf"&gt;runWhenAllowed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The cost.&lt;/strong&gt; In the worst case the scan starts with a delay of up to ~30 seconds. The system counts the limit per app, so if something else in the app is scanning too, our counter may not match the system's. That is what the next item is for.&lt;/p&gt;

&lt;h3&gt;
  
  
  Silent rejection when the quota is exceeded
&lt;/h3&gt;

&lt;p&gt;If the system rejected a start, &lt;code&gt;onScanFailed&lt;/code&gt; may never arrive. The app thinks the scan is running, but there are no results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How we fixed it.&lt;/strong&gt; We added a watchdog. It remembers when the last result arrived and, if the silence lasts longer than a threshold, restarts the scan through the same quota counter. That way the watchdog can't run into the limit itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="no"&gt;SILENCE_MS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60_000L&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lastResultAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0L&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onScanResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ScanResult&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;lastResultAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SystemClock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;elapsedRealtime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;deliver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;watchdogTask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Runnable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SystemClock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;elapsedRealtime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lastResultAt&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;SILENCE_MS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;restart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postDelayed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SILENCE_MS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The cost.&lt;/strong&gt; Silence doesn't always mean failure: the person may simply have walked out of beacon range. Then we restart the scan once a minute for nothing. It is almost free for the system, but the threshold should be tuned to your use case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Process death and swiping from Recents
&lt;/h3&gt;

&lt;p&gt;The system can kill the process when memory is low or, starting with Android 17, when the app exceeds its memory limit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How we fixed it.&lt;/strong&gt; The service returns &lt;code&gt;START_STICKY&lt;/code&gt;, and the system restarts it by itself. This is where we hit a pitfall. In the old version, the scan was started on &lt;code&gt;ON_START&lt;/code&gt; and &lt;code&gt;ON_STOP&lt;/code&gt; events from &lt;code&gt;ProcessLifecycleOwner&lt;/code&gt;. If the process came up without UI (which is exactly what happens on a &lt;code&gt;START_STICKY&lt;/code&gt; restart), these events never arrived, and the scan didn't start until the user opened the app. Now the scan starts immediately, and the mode is chosen from the app's current state. Lifecycle events are only used to switch modes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ScanService&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Service&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onStartCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;startId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;startForeground&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NOTIFICATION_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;notification&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FOREGROUND_SERVICE_TYPE_LOCATION&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;scanController&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// both on first launch and after a restart (intent == null)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;START_STICKY&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;BleScanController&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;visible&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ProcessLifecycleOwner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lifecycle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;currentState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isAtLeast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;STARTED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;mode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;visible&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;FOREGROUND&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="no"&gt;BACKGROUND&lt;/span&gt;
    &lt;span class="nf"&gt;requestStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// don't wait for ON_START / ON_STOP&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;kill -9&lt;/code&gt; test, the system restarted the service in 1.7 s, and the first detection arrived after 2.1 s.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The cost.&lt;/strong&gt; &lt;code&gt;START_STICKY&lt;/code&gt; is not a silver bullet. The system may increase the delay between restarts after repeated crashes, and some firmware won't bring the service back at all without the autostart permission.&lt;/p&gt;

&lt;h3&gt;
  
  
  Background process restrictions
&lt;/h3&gt;

&lt;p&gt;Without a Foreground Service, a background process gets stopped sooner or later. And since Android 12, the service itself can't be started from the background.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How we fixed it.&lt;/strong&gt; We start a Foreground Service of type &lt;code&gt;location&lt;/code&gt; while the app is open. After that the service lives on its own and survives minimizing and swiping from Recents. In our device tests, the gap in the stream after a swipe was 0.3 s.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// in an Activity, while the app is on screen&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;hasScanPermissions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ContextCompat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startForegroundService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ScanService&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The cost.&lt;/strong&gt; On Android 13+ you have to request the runtime notification permission, and not every user likes seeing a permanent notification in the shade.&lt;/p&gt;

&lt;h3&gt;
  
  
  The background scan mode gets downgraded
&lt;/h3&gt;

&lt;p&gt;The system moves background apps to power-saving modes. We used to switch to &lt;code&gt;LOW_POWER&lt;/code&gt; in the background on our side too, which gives about 10 detections per minute with the screen on and about 6 with it off.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How we fixed it.&lt;/strong&gt; While a Foreground Service is running, the system keeps &lt;code&gt;LOW_LATENCY&lt;/code&gt; in the background as well, as long as the scan has a filter. So our SDK has a flag that keeps &lt;code&gt;LOW_LATENCY&lt;/code&gt; after the app is minimized. Of course, you still won't get the screen-on rate: in our tests the controller listens about 52% of the time in this case and delivers about 48 detections per minute instead of 90. That is the price of stable operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ScanSettings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ScanSettings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setScanMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;FOREGROUND&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lowLatencyInBackground&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;SCAN_MODE_LOW_LATENCY&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="no"&gt;SCAN_MODE_LOW_POWER&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The cost.&lt;/strong&gt; This is the most battery-hungry part of the setup: the radio is working almost all the time. That is exactly why it is an option, not the default: if you only need a position every few seconds, &lt;code&gt;LOW_POWER&lt;/code&gt; is enough.&lt;/p&gt;

&lt;h3&gt;
  
  
  WorkManager and JobScheduler: no more than once every 15 minutes
&lt;/h3&gt;

&lt;p&gt;Periodic jobs run no more often than once every 15 minutes, and since Android 16 quotas apply to them even while a service is running. That doesn't work for our task.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How we fixed it.&lt;/strong&gt; The scan itself runs only in the service. A periodic job checks every 15 minutes whether the service is alive and how long ago the last result came in. If the service is dead, the job tries to bring it back, and if the system won't allow starting the service from the background, it turns on a PendingIntent scan as a fallback.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ScanGuardWorker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;WorkerParameters&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ScanService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isAliveAndReceiving&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;ContextCompat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startForegroundService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;applicationContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;applicationContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ScanService&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ForegroundServiceStartNotAllowedException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;startPendingIntentScan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;applicationContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// fallback&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// registration&lt;/span&gt;
&lt;span class="nc"&gt;WorkManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueueUniquePeriodicWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;"scan-guard"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ExistingPeriodicWorkPolicy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;KEEP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;PeriodicWorkRequestBuilder&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ScanGuardWorker&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TimeUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;MINUTES&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The cost.&lt;/strong&gt; Up to 15 minutes pass before the safety net kicks in, and in Doze it can be even longer. On Android 12+, a Foreground Service can be started from the background only in specific cases, for example when the app is excluded from battery optimization or a high-priority push arrives. Otherwise &lt;code&gt;ForegroundServiceStartNotAllowedException&lt;/code&gt; is thrown. The system downgrades a background PendingIntent scan too, so it is a safety net, not a replacement for the service.&lt;/p&gt;

&lt;h3&gt;
  
  
  Doze, standby buckets, battery settings and permissions
&lt;/h3&gt;

&lt;p&gt;These restrictions deserve a separate item, because unfortunately there is no way around them in code. All you can do is notice them in time and ask the user for help.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Doze.&lt;/strong&gt; When the phone lies still and isn't charging, the system defers network access, alarms and jobs. A service won't protect you from that. One possible approach is to buffer the data and upload it to the server once the network is available again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Standby bucket.&lt;/strong&gt; While a Foreground Service is running, the app counts as active. But if the service has died and the app hasn't been opened for a long time, the system moves it to a stricter bucket, &lt;code&gt;RESTRICTED&lt;/code&gt; (Android 12+), and our worker-based safety net will run rarely. You can check the bucket with &lt;code&gt;UsageStatsManager.getAppStandbyBucket()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Battery optimization.&lt;/strong&gt; An app excluded from battery optimization suffers less from Doze and can start a Foreground Service from the background. You can check this with &lt;code&gt;isIgnoringBatteryOptimizations()&lt;/code&gt; and, if needed, open the settings for the user. Google Play allows the direct &lt;code&gt;REQUEST_IGNORE_BATTERY_OPTIMIZATIONS&lt;/code&gt; request only for apps whose core function is broken by Doze and App Standby. For firmware with its own settings ("Never sleeping apps" on Samsung, autostart on Xiaomi), it is better to show vendor-specific instructions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Permissions and device state.&lt;/strong&gt; Before starting, we check everything that can silently stop the scan and tell the app exactly what is wrong. We catch Bluetooth being turned off and on via &lt;code&gt;ACTION_STATE_CHANGED&lt;/code&gt; and restart the scan through the quota counter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;diagnose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Problem&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buildList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;bluetoothEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BLUETOOTH_OFF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SDK_INT&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;granted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BLUETOOTH_SCAN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NO_BLUETOOTH_SCAN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;granted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ACCESS_FINE_LOCATION&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NO_FINE_LOCATION&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;granted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ACCESS_BACKGROUND_LOCATION&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NO_BACKGROUND_LOCATION&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;locationServicesEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;LOCATION_OFF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SDK_INT&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;33&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;granted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;POST_NOTIFICATIONS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NO_NOTIFICATIONS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;powerManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isIgnoringBatteryOptimizations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;packageName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BATTERY_OPTIMIZED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usageStats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;appStandbyBucket&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="no"&gt;STANDBY_BUCKET_RARE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;STANDBY_RESTRICTED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep in mind that every request to the user is one more setup screen and one more chance they skip it. In enterprise deployments this is often solved by configuring devices via MDM; in consumer apps, only by a really good explanation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Of course, none of this is an absolute guarantee that scanning will work perfectly. With every new Android version and every new firmware, you have to find ever more creative ways to live with the restrictions. The scan timeout itself is a server-side setting and can change without a system update, and since Android 16 the Bluetooth module is updated separately from the firmware.&lt;/p&gt;

&lt;p&gt;So the main thing we took away from this story is the habit of measuring. Things in the system rarely break loudly, and if you don't measure the stream of results, you may first hear about the problem from a customer.&lt;/p&gt;

&lt;p&gt;On our phone, the main problem turned out to be the long-running scan timeout: after 5 minutes the system downgrades the mode, and you get 5–10 times fewer detections. The app never finds out: formally the scan is running and callbacks are coming in, just rarely.&lt;/p&gt;

&lt;p&gt;In short, reliable background scanning needs four things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A real filter for your beacon format. An empty filter doesn't work with the screen off.&lt;/li&gt;
&lt;li&gt;Restarting the scan before the timeout, through a single start counter so you don't hit the limit.&lt;/li&gt;
&lt;li&gt;Monitoring the results stream. You can't rely on &lt;code&gt;onScanFailed&lt;/code&gt;; if there are no results for a long time, restart the scan.&lt;/li&gt;
&lt;li&gt;A Foreground Service that brings the scan back right after a process restart, without waiting for the UI to open.
Before you choose your intervals, check how long the timeout is on your devices:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;adb shell device_config get bluetooth scan_timeout_millis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'd love it if you shared the values from your phones in the comments.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>mobile</category>
      <category>bluetooth</category>
    </item>
  </channel>
</rss>
