Skip to content

RF Sentinel (RFSentinel): free, open-source police detector app for Android. Spots police body cams, Flock ALPR cameras, AirTag trackers, Remote ID drones, smart glasses and fake cell towers from Bluetooth LE and WiFi. No root.

Raw GPS backstop when fused location stays silent; restart a throttled BLE scan

CIS-C0aa12afc48c8b
diff --git a/app/src/main/java/com/rfsentinel/app/service/BleRateWatch.kt b/app/src/main/java/com/rfsentinel/app/service/BleRateWatch.kt
new file mode 100644
index 0000000..daa7d28
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/service/BleRateWatch.kt
@@ -0,0 +1,42 @@
+package com.rfsentinel.app.service
+
+/**
+ * Spots a Bluetooth scan Android has quietly throttled (after Wardrive Go's BLE watchdog): the
+ * scan still runs and a few results still trickle in, so the silence watchdog never fires, but
+ * the rate has collapsed to a fraction of what it was a moment ago. Restarting the scan brings
+ * it back. Fed only while the screen stays on (screen-off rates are lower by design).
+ */
+class BleRateWatch {
+ /** Results per second while the scan is healthy (a slow average); null while learning. */
+ private var baseline: Double? = null
+ private var lowTicks = 0
+ private var lastRestartAt = -MIN_GAP_MS
+
+ /** [count] results over [ms]. True: restart the scan now. */
+ fun tick(count: Int, ms: Long, now: Long): Boolean {
+ if (ms <= 0) return false
+ val rate = count * 1000.0 / ms
+ val b = baseline ?: run { baseline = rate; return false }
+ if (b >= MIN_BASELINE && rate < b * COLLAPSED) {
+ if (++lowTicks < LOW_TICKS || now - lastRestartAt < MIN_GAP_MS) return false
+ // Relearn after the restart: if the area really is this quiet, that's the new normal.
+ lowTicks = 0; lastRestartAt = now; baseline = null
+ return true
+ }
+ lowTicks = 0
+ baseline = b * 0.8 + rate * 0.2
+ return false
+ }
+
+ fun reset() { baseline = null; lowTicks = 0 }
+
+ companion object {
+ /** Below this many results a second there's too little to tell a collapse from a quiet street. */
+ const val MIN_BASELINE = 3.0
+ /** A tenth of the usual rate or less... */
+ const val COLLAPSED = 0.1
+ /** ...for this many ticks in a row. */
+ const val LOW_TICKS = 2
+ const val MIN_GAP_MS = 5 * 60_000L
+ }
+}
diff --git a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
index 5da07cd..d1ea2fd 100644
--- a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
+++ b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
@@ -69,6 +69,9 @@ class ScanForegroundService : Service() {
private const val RADAR_HOLD_MS = 120_000L
/** "GPS connected" again only after the fix was lost this long. */
private const val GPS_RECONNECT_MS = 120_000L
+ /** No fused fix this long after asking (precise / balanced): raw GPS and network fill in. */
+ private const val FUSED_SILENT_FAST_MS = 15_000L
+ private const val FUSED_SILENT_MS = 30_000L
private const val TAG = "ScanForegroundService"
const val ACTION_STOP = "com.rfsentinel.app.action.STOP"
@@ -196,7 +199,29 @@ class ScanForegroundService : Service() {
/** Radar-detector sound style: last good GPS fix, to say "GPS connected" when it locks or comes back. */
private var lastGoodGpsAt = 0L
+ /**
+ * Fused location can stay silent on a phone without Google's location services: if it hasn't
+ * given a single fix by [backstopCheck], raw GPS / network fill in until it does.
+ */
+ private var fusedEverFixed = false
+ private var backstopOn = false
+ private val backstopListener = LocationListenerCompat { onFix(it) }
+ private val backstopCheck = Runnable { engageBackstop() }
+
private val locationListener = LocationListenerCompat {
+ if (it.provider == LocationManager.FUSED_PROVIDER && !fusedEverFixed) {
+ fusedEverFixed = true
+ locatingHandler.removeCallbacks(backstopCheck)
+ if (backstopOn) {
+ Log.i(TAG, "Fused location is giving fixes: raw GPS backstop off")
+ runCatching { (getSystemService(LOCATION_SERVICE) as LocationManager).removeUpdates(backstopListener) }
+ backstopOn = false
+ }
+ }
+ onFix(it)
+ }
+
+ private fun onFix(it: Location) {
if (it.provider == LocationManager.GPS_PROVIDER && it.hasAccuracy() && it.accuracy <= 50f) {
val now = System.currentTimeMillis()
if (now - lastGoodGpsAt > GPS_RECONNECT_MS) AlertPlayer.gpsConnected(this)
@@ -234,6 +259,7 @@ class ScanForegroundService : Service() {
bleEngine = BleScanEngine(this) { r ->
val t = System.currentTimeMillis()
lastBleResultAt = t
+ bleResults.incrementAndGet()
bleWatchdogRestartAt?.let { onRecovered("Bluetooth", it, t); bleWatchdogRestartAt = null; bleSilenceMs = BLE_SILENCE_MS }
pipeline.post { AdvertFactory.fromBle(r)?.let(::processPhone) }
}
@@ -443,46 +469,74 @@ class ScanForegroundService : Service() {
// (at highway speed a 20 s interval could skip right past one).
val fast = TripRecorder.isRecording || knownAlprActive() || mapVisible || locating
val mode = when { locating -> 2; fast -> 1; else -> 0 }
- val intervalMs = if (locating) 1_000L else 3_000L
val lm = getSystemService(LOCATION_SERVICE) as LocationManager
if (locationListening && (!wanted || mode != locationMode)) {
- lm.removeUpdates(locationListener)
- locationListening = false
+ stopLocationUpdates(lm)
}
if (wanted && !locationListening) {
locationMode = mode
- val providers = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
- lm.hasProvider(LocationManager.FUSED_PROVIDER)
- ) {
- listOf(LocationManager.FUSED_PROVIDER)
- } else {
- listOf(LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER).filter { lm.isProviderEnabled(it) }
- }
- providers.forEach { p ->
- try {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
- // Fused location defaults to "balanced" (often WiFi/cell, ~100 m, and
- // paused while it thinks you're stationary): too coarse to warn about a
- // camera a few hundred metres ahead. Fast mode asks for real GPS.
- val request = android.location.LocationRequest.Builder(if (fast) intervalMs else 20_000L)
- .setQuality(
- if (fast) android.location.LocationRequest.QUALITY_HIGH_ACCURACY
- else android.location.LocationRequest.QUALITY_BALANCED_POWER_ACCURACY
- )
- .setMinUpdateDistanceMeters(if (locating) 0f else if (fast) 5f else 25f)
- .build()
- lm.requestLocationUpdates(p, request, mainExecutor, locationListener)
- } else if (fast) lm.requestLocationUpdates(p, intervalMs, if (locating) 0f else 5f, locationListener, Looper.getMainLooper())
- else lm.requestLocationUpdates(p, 20_000L, 25f, locationListener, Looper.getMainLooper())
- lm.getLastKnownLocation(p)?.let { if (lastLocation == null) lastLocation = it }
- } catch (e: Exception) {
- Log.w(TAG, "Location provider $p unavailable", e)
+ val fused = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && lm.hasProvider(LocationManager.FUSED_PROVIDER)
+ if (fused) {
+ requestFixes(lm, listOf(LocationManager.FUSED_PROVIDER), locationListener)
+ when {
+ backstopOn -> requestFixes(lm, rawProviders(lm), backstopListener)
+ // The first fused fix can take a while on a balanced, 20 s request.
+ !fusedEverFixed -> locatingHandler.postDelayed(backstopCheck, if (fast) FUSED_SILENT_FAST_MS else FUSED_SILENT_MS)
}
- }
+ } else requestFixes(lm, rawProviders(lm), locationListener)
locationListening = true
}
}
+ private fun rawProviders(lm: LocationManager) =
+ listOf(LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER).filter { lm.isProviderEnabled(it) }
+
+ @SuppressLint("MissingPermission") // only called from updateLocationUpdates(), which checks it
+ private fun requestFixes(lm: LocationManager, providers: List<String>, listener: LocationListenerCompat) {
+ val locating = locationMode == 2
+ val fast = locationMode >= 1
+ val intervalMs = if (locating) 1_000L else 3_000L
+ providers.forEach { p ->
+ try {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
+ // Fused location defaults to "balanced" (often WiFi/cell, ~100 m, and
+ // paused while it thinks you're stationary): too coarse to warn about a
+ // camera a few hundred metres ahead. Fast mode asks for real GPS.
+ val request = android.location.LocationRequest.Builder(if (fast) intervalMs else 20_000L)
+ .setQuality(
+ if (fast) android.location.LocationRequest.QUALITY_HIGH_ACCURACY
+ else android.location.LocationRequest.QUALITY_BALANCED_POWER_ACCURACY
+ )
+ .setMinUpdateDistanceMeters(if (locating) 0f else if (fast) 5f else 25f)
+ .build()
+ lm.requestLocationUpdates(p, request, mainExecutor, listener)
+ } else if (fast) lm.requestLocationUpdates(p, intervalMs, if (locating) 0f else 5f, listener, Looper.getMainLooper())
+ else lm.requestLocationUpdates(p, 20_000L, 25f, listener, Looper.getMainLooper())
+ lm.getLastKnownLocation(p)?.let { if (lastLocation == null) lastLocation = it }
+ } catch (e: Exception) {
+ Log.w(TAG, "Location provider $p unavailable", e)
+ }
+ }
+ }
+
+ /** No fused fix yet: GPS and network join in (fused stays on, and takes over at its first fix). */
+ private fun engageBackstop() {
+ if (!locationListening || fusedEverFixed || backstopOn || !hasFineLocation()) return
+ val lm = getSystemService(LOCATION_SERVICE) as LocationManager
+ val raw = rawProviders(lm)
+ if (raw.isEmpty()) { Log.w(TAG, "No fused fix yet, and GPS and network location are both off: nothing to fall back on"); return }
+ Log.i(TAG, "No fused fix yet: raw ${raw.joinToString("+")} location on as a backstop")
+ backstopOn = true
+ requestFixes(lm, raw, backstopListener)
+ }
+
+ private fun stopLocationUpdates(lm: LocationManager) {
+ locatingHandler.removeCallbacks(backstopCheck)
+ runCatching { lm.removeUpdates(locationListener) }
+ runCatching { lm.removeUpdates(backstopListener) } // backstopOn stays: re-requested with the next mode
+ locationListening = false
+ }
+
// ---- Pipeline -------------------------------------------------------------
/** Something the phone's own Bluetooth or Wi-Fi chip heard. */
@@ -858,6 +912,11 @@ class ScanForegroundService : Service() {
/** Silence allowed before a restart; doubles after each restart that brought nothing back. */
@Volatile private var bleSilenceMs = BLE_SILENCE_MS
@Volatile private var wifiSilenceMs = WIFI_SILENCE_MS
+ /** Bluetooth results since the last watchdog tick, for [bleRate]. */
+ private val bleResults = java.util.concurrent.atomic.AtomicInteger()
+ private val bleRate = BleRateWatch()
+ private var rateTickAt = 0L
+ private var wasInteractive = false
/** Results came back after a restart: the scanner really was stuck, so say so. */
private fun onRecovered(which: String, restartAt: Long, now: Long) {
@@ -889,6 +948,19 @@ class ScanForegroundService : Service() {
bleWatchdogRestartAt = now
android.os.Handler(Looper.getMainLooper()).post { bleEngine.stop(); startBle() }
}
+ // Throttled rather than silent: results still trickle in, at a tenth of the usual rate.
+ val count = bleResults.getAndSet(0)
+ val span = now - rateTickAt
+ rateTickAt = now
+ if (interactive && wasInteractive && adapterOn && Prefs.bleEnabled(this) && bleEngine.isScanning() &&
+ bleWatchdogRestartAt != now // already restarted for silence
+ ) {
+ if (bleRate.tick(count, span, now)) {
+ Log.w(TAG, "Watchdog: Bluetooth results down to %.1f/s - restarting the scan".format(count * 1000.0 / span))
+ android.os.Handler(Looper.getMainLooper()).post { bleEngine.stop(); startBle() }
+ }
+ } else bleRate.reset()
+ wasInteractive = interactive
val wifiOn = runCatching { (applicationContext.getSystemService(WIFI_SERVICE) as android.net.wifi.WifiManager).isScanAlwaysAvailable ||
(applicationContext.getSystemService(WIFI_SERVICE) as android.net.wifi.WifiManager).isWifiEnabled }.getOrDefault(false)
if (wifiOn && Prefs.wifiEnabled(this) &&
@@ -1298,10 +1370,7 @@ class ScanForegroundService : Service() {
try { unregisterReceiver(btStateReceiver) } catch (_: Exception) {}
btStateReceiverRegistered = false
}
- if (locationListening) {
- runCatching { (getSystemService(LOCATION_SERVICE) as LocationManager).removeUpdates(locationListener) }
- locationListening = false
- }
+ if (locationListening) stopLocationUpdates(getSystemService(LOCATION_SERVICE) as LocationManager)
AlertPlayer.stopIntro()
serviceScope.cancel()
pipelineThread.quitSafely()
diff --git a/app/src/test/java/com/rfsentinel/app/service/BleRateWatchTest.kt b/app/src/test/java/com/rfsentinel/app/service/BleRateWatchTest.kt
new file mode 100644
index 0000000..04eed05
--- /dev/null
+++ b/app/src/test/java/com/rfsentinel/app/service/BleRateWatchTest.kt
@@ -0,0 +1,47 @@
+package com.rfsentinel.app.service
+
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class BleRateWatchTest {
+
+ private fun BleRateWatch.feed(perSecond: Int, ticks: Int, start: Long): Pair<Boolean, Long> {
+ var t = start; var fired = false
+ repeat(ticks) { t += 10_000; if (tick(perSecond * 10, 10_000, t)) fired = true }
+ return fired to t
+ }
+
+ @Test
+ fun restartsWhenTheRateCollapses() {
+ val w = BleRateWatch()
+ val (_, t) = w.feed(40, 6, 0)
+ assertFalse("one low tick is not enough", w.tick(20, 10_000, t + 10_000))
+ assertTrue(w.tick(20, 10_000, t + 20_000))
+ }
+
+ @Test
+ fun aQuietAreaIsNotACollapse() {
+ val w = BleRateWatch()
+ val (fired, t) = w.feed(2, 6, 0)
+ assertFalse(fired)
+ assertFalse("below the minimum baseline", w.feed(0, 3, t).first)
+ }
+
+ @Test
+ fun relearnsAfterARestartAndWaitsBetweenRestarts() {
+ val w = BleRateWatch()
+ var (_, t) = w.feed(40, 6, 0)
+ assertTrue(w.feed(1, 2, t).first); t += 20_000
+ // Still quiet after the restart: that's the new normal, not another collapse.
+ val (fired, _) = w.feed(1, 30, t)
+ assertFalse(fired)
+ }
+
+ @Test
+ fun aDropThatHalvesTheRateIsFine() {
+ val w = BleRateWatch()
+ val (_, t) = w.feed(40, 6, 0)
+ assertFalse(w.feed(20, 10, t).first)
+ }
+}