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.
Kotlin airtagalprandroidandroid-autoanti-surveillancebluetooth-low-energybody-cameracounter-surveillancedrone-detectionflock-safetyimsi-catcher-detectorpolice-detectorpolice-scannerprivacyremote-idrtl-sdrsdrsurveillance-detectiontracker-detectionwifi
Quicker sweeps: bridge single skipped sectors, least-squares fit, 20 Hz sampling, game-rate compass, 1 s settle
CIS-C0a3a18c49d6db
diff --git a/app/src/main/java/com/rfsentinel/app/ui/Compass.kt b/app/src/main/java/com/rfsentinel/app/ui/Compass.ktindex 3d4f0ed..77ea95d 100644--- a/app/src/main/java/com/rfsentinel/app/ui/Compass.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/Compass.kt@@ -37,7 +37,8 @@ class Compass(context: Context) : SensorEventListener {fun start() {if (running || sensor == null) return- running = sm?.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI) == true+ // Game rate: the heading keeps up with a brisk turn (a sweep pairs it with each packet).+ running = sm?.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME) == true}fun stop() {diff --git a/app/src/main/java/com/rfsentinel/app/ui/LiveRead.kt b/app/src/main/java/com/rfsentinel/app/ui/LiveRead.ktindex 1e29668..52e693f 100644--- a/app/src/main/java/com/rfsentinel/app/ui/LiveRead.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/LiveRead.kt@@ -41,7 +41,8 @@ object LiveRead {)private const val TICK_MS = 1_000L- private const val SAMPLE_MS = 200L+ /** Quick enough to catch nearly every packet of a fast-advertising device during a brisk turn. */+ private const val SAMPLE_MS = 50L/** Walked this far since the lock: the direction is old, sweep again. */const val SWEEP_STALE_M = 25f/** Heard within this: its signal is live. */@@ -164,7 +165,7 @@ object LiveRead {}/**- * Five times a second: each new raw signal reading with the way the phone faced when it was heard. Locks the+ * Twenty times a second: each new raw signal reading with the way the phone faced when it was heard. Locks the* direction once the turn makes it clear and it holds, with a short buzz.*/private val sample = object : Runnable {diff --git a/app/src/main/java/com/rfsentinel/app/ui/RadarView.kt b/app/src/main/java/com/rfsentinel/app/ui/RadarView.ktindex 8d8d040..5ba8312 100644--- a/app/src/main/java/com/rfsentinel/app/ui/RadarView.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/RadarView.kt@@ -68,7 +68,7 @@ class RadarView @JvmOverloads constructor(private var tracking = false/**- * Five times a second while on screen: each alert's new raw reading with the way you faced when it was heard,+ * Twenty times a second while on screen: each alert's new raw reading with the way you faced when it was heard,* so [RadarTrack] finds its side as you turn and keeps it placed as you walk ([Motion] tracks your steps).*/private val sampler = object : Runnable {@@ -345,7 +345,7 @@ class RadarView @JvmOverloads constructor(override fun performClick(): Boolean = super.performClick()private companion object {- const val SAMPLE_MS = 200L+ const val SAMPLE_MS = 50L/** Faster than a brisk walk (m/s): driving. */const val WALK_MS = 3f}diff --git a/app/src/main/java/com/rfsentinel/app/ui/Sweep.kt b/app/src/main/java/com/rfsentinel/app/ui/Sweep.ktindex 8040ee8..01e3cc0 100644--- a/app/src/main/java/com/rfsentinel/app/ui/Sweep.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/Sweep.kt@@ -34,8 +34,15 @@ class Sweep {fun clear() { readings.clear(); candidate = null }- /** Sectors with a reading in the last [WINDOW_MS] (out of [SECTORS]). */- fun covered(now: Long) = sectors(now).count { it != null }+ /**+ * Sectors of the turn covered in the last [WINDOW_MS] (out of [SECTORS]): those with a reading, and a single+ * one skipped between two with readings (a quicker turn passes a sector between two packets).+ */+ fun covered(now: Long) = covered(sectors(now))++ private fun covered(m: Array<Float?>) = m.indices.count { i ->+ m[i] != null || (m[(i + SECTORS - 1) % SECTORS] != null && m[(i + 1) % SECTORS] != null)+ }/*** The median signal (dBm) heard facing each sector, null where you haven't faced yet. Sector i is centred on@@ -48,21 +55,23 @@ class Sweep {}/**- * Fits one rise and fall over the turn (the first harmonic of the signal against the heading): its peak is the- * direction. Null until [MIN_SECTORS] are covered.+ * Fits one rise and fall over the turn (the first harmonic of the signal against the heading, least squares+ * over the sectors heard, so skipped ones don't tilt it): its peak is the direction. Null until [MIN_SECTORS]+ * are covered, [MIN_HEARD] of them heard.*/fun estimate(now: Long): Estimate? {val m = sectors(now)val k = m.indices.filter { m[it] != null }- if (k.size < MIN_SECTORS) return null- val mean = k.sumOf { m[it]!!.toDouble() } / k.size- var a = 0.0; var b = 0.0+ val covered = covered(m)+ if (covered < MIN_SECTORS || k.size < MIN_HEARD) return null+ // Normal equations for m = c + a cos t + b sin t.+ val n = Array(3) { DoubleArray(4) }for (i in k) {val t = Math.toRadians(centre(i).toDouble())- val d = m[i]!! - mean- a += d * cos(t); b += d * sin(t)+ val f = doubleArrayOf(1.0, cos(t), sin(t))+ for (r in 0..2) { for (c in 0..2) n[r][c] += f[r] * f[c]; n[r][3] += f[r] * m[i]!! }}- a *= 2.0 / k.size; b *= 2.0 / k.size+ val (mean, a, b) = solve3(n) ?: return nullval amp = hypot(a, b)val peak = atan2(b, a)// What the one rise and fall leaves unexplained: fading and reflections.@@ -75,10 +84,24 @@ class Sweep {val weakest = k.minOf { m[it]!! }val behind = k.filter { abs(turn(bearing + 180f, centre(it))) <= NULL_SLACK_DEG }.minOfOrNull { m[it]!! }val nullFactor = if (behind != null && behind <= weakest + NULL_TOLERANCE_DB) 1f else 0.5f- val confidence = k.size.toFloat() / SECTORS * (amp / (amp + rest + NOISE_DB)).toFloat() * nullFactor+ val confidence = covered.toFloat() / SECTORS * (amp / (amp + rest + NOISE_DB)).toFloat() * nullFactorreturn Estimate(bearing, confidence.coerceIn(0f, 1f), (2 * amp).toFloat())}+ /** Gaussian elimination of a 3 x 3 system (augmented); null when singular. */+ private fun solve3(n: Array<DoubleArray>): DoubleArray? {+ for (c in 0..2) {+ val p = (c..2).maxBy { abs(n[it][c]) }+ if (abs(n[p][c]) < 1e-9) return null+ n[c] = n[p].also { n[p] = n[c] }+ for (r in 0..2) if (r != c) {+ val f = n[r][c] / n[c][c]+ for (j in c..3) n[r][j] -= f * n[c][j]+ }+ }+ return DoubleArray(3) { n[it][3] / n[it][it] }+ }+private var candidate: Estimate? = nullprivate var candidateAt = 0L@@ -98,6 +121,8 @@ class Sweep {const val SECTOR_DEG = 360f / SECTORS/** A turn covers this many sectors (300°) before a direction is given. */const val MIN_SECTORS = 10+ /** Sectors with their own reading among those: the rest bridged single gaps. */+ const val MIN_HEARD = 7/** Readings older than this drop out, so walking somewhere new starts a fresh sweep. */const val WINDOW_MS = 45_000L/** Locked once this sure, and the body shadow shows by at least [LOCK_MIN_DB]. */@@ -109,7 +134,7 @@ class Sweep {private const val NULL_SLACK_DEG = 60fprivate const val NULL_TOLERANCE_DB = 1fconst val SETTLE_DEG = 20f- const val SETTLE_MS = 1_500L+ const val SETTLE_MS = 1_000Lfun centre(sector: Int) = sector * SECTOR_DEG + SECTOR_DEG / 2diff --git a/app/src/test/java/com/rfsentinel/app/ui/SweepTest.kt b/app/src/test/java/com/rfsentinel/app/ui/SweepTest.ktindex 10bf7ed..e333bf0 100644--- a/app/src/test/java/com/rfsentinel/app/ui/SweepTest.kt+++ b/app/src/test/java/com/rfsentinel/app/ui/SweepTest.kt@@ -43,6 +43,17 @@ class SweepTest {assertEquals(0, flat.covered(1_000L + Sweep.WINDOW_MS + 1))}+ @Test+ fun quickTurnWithAPacketEvery40DegreesStillLocks() {+ val s = Sweep()+ // One turn in about 4 s: a packet only every 40°, so a few sectors are skipped.+ for (h in 0 until 360 step 40) s.add(1_000L + h * 10, h.toFloat(), (-75 + 5 * cos(Math.toRadians(h - 200.0))).toInt())+ assertEquals(12, s.covered(5_000L))+ val e = s.estimate(5_000L)!!+ assertEquals(200f, e.bearing, 15f)+ assertTrue(e.toString(), Sweep.locks(e))+ }+@Testfun locksOnlyOnceItHolds() {val s = Sweep()@@ -50,8 +61,8 @@ class SweepTest {assertNull(s.settled(19_000L)) // just became clears.add(19_500L, 120f, -70)assertNull(s.settled(19_500L))- s.add(21_000L, 125f, -70)- assertEquals(120f, s.settled(21_000L)!!.bearing, 15f)+ s.add(20_500L, 125f, -70)+ assertEquals(120f, s.settled(20_500L)!!.bearing, 15f)}@Test