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.

Sweep: start only upright on the chest, silence facing away counts as the shadow, sectors kept while standing still, strong side weighs more

CIS-C02d55fef27875
diff --git a/app/src/main/java/com/rfsentinel/app/ui/Compass.kt b/app/src/main/java/com/rfsentinel/app/ui/Compass.kt
index 77ea95d..c421111 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/Compass.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/Compass.kt
@@ -23,7 +23,9 @@ class Compass(context: Context) : SensorEventListener {
private val m = FloatArray(9)
private val r = FloatArray(9)
private val o = FloatArray(3)
- private var upright = false
+ /** Held upright (screen facing you, as on your chest), not flat. */
+ var upright = false
+ private set
private var declination = 0f
private var running = false
diff --git a/app/src/main/java/com/rfsentinel/app/ui/LiveRead.kt b/app/src/main/java/com/rfsentinel/app/ui/LiveRead.kt
index 52e693f..538e336 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/LiveRead.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/LiveRead.kt
@@ -45,6 +45,8 @@ object LiveRead {
private const val SAMPLE_MS = 50L
/** Walked this far since the lock: the direction is old, sweep again. */
const val SWEEP_STALE_M = 25f
+ /** Walked this far before it locked: the sweep starts over where you are. */
+ private const val SWEEP_MOVE_M = 5.0
/** Heard within this: its signal is live. */
private const val HEARD_MS = 10_000L
/** A pinpointed device unheard this long gives way to another flagged one. */
@@ -73,13 +75,15 @@ object LiveRead {
private var sweepFed = 0L
private var tickAt = 0L
private var tickHeading: Float? = null
+ /** Where this sweep's readings started: walking off starts it over. */
+ private var sweepFrom: Motion.Pos? = null
/** The direction the sweep locked on (kept until Sweep again or Locate stops), and where you stood then. */
private var lock: Sweep.Estimate? = null
private var lockedAt: android.location.Location? = null
/** The sweep as the Locate guide draws it. [moved]: metres walked since it locked. */
class SweepState(val heading: Float?, val sectors: Array<Float?>, val covered: Int,
- val estimate: Sweep.Estimate?, val locked: Sweep.Estimate?, val moved: Float?)
+ val estimate: Sweep.Estimate?, val locked: Sweep.Estimate?, val moved: Float?, val upright: Boolean)
// ---- Pinpoint (the scan's pipeline thread) ----
private var pinMac: String? = null
@@ -96,7 +100,7 @@ object LiveRead {
appContext = context.applicationContext
locating = mac
followed = null
- sweep.clear(); sweepFed = 0L; lock = null; lockedAt = null
+ sweep.clear(); sweepFed = 0L; lock = null; lockedAt = null; sweepFrom = null
compass = Compass(context).also { c ->
com.rfsentinel.app.service.ScanForegroundService.lastFix?.let { c.setLocation(it.latitude, it.longitude, it.altitude) }
c.start()
@@ -109,7 +113,7 @@ object LiveRead {
/** Forgets the sweep and its lock, for a fresh turn (somewhere new, or a lock that looks wrong). Main thread. */
fun sweepAgain() {
- sweep.clear(); sweepFed = 0L; lock = null; lockedAt = null
+ sweep.clear(); sweepFed = 0L; lock = null; lockedAt = null; sweepFrom = null
tick()
}
@@ -120,7 +124,7 @@ object LiveRead {
// Placed (it stays put as you walk): no need to sweep again.
val moved = if (RadarTrack.place(mac, now) != null) null
else lockedAt?.let { a -> com.rfsentinel.app.service.ScanForegroundService.lastFix?.distanceTo(a) }
- return SweepState(heading, sweep.sectors(now), sweep.covered(now), sweep.estimate(now), held(now), moved)
+ return SweepState(heading, sweep.sectors(now), sweep.covered(now), sweep.estimate(now), held(now), moved, compass?.upright == true)
}
/** The lock, turned to where the device is from where you stand now once [RadarTrack] placed it. */
@@ -176,11 +180,20 @@ object LiveRead {
val now = System.currentTimeMillis()
val t0 = tickAt; val h0 = tickHeading ?: h
tickAt = now; tickHeading = h
+ // Only on your chest: held flat it isn't the sweep the guide shows.
+ if (compass?.upright != true) return
+ val pos = Motion.position
+ val from = sweepFrom
+ if (lock == null && pos != null && from != null && kotlin.math.hypot(pos.x - from.x, pos.y - from.y) > SWEEP_MOVE_M) {
+ sweep.clear(); sweepFrom = null
+ }
+ sweep.face(now, h)
val (seen, a) = DeviceRegistry.lastHeard(followed ?: mac) ?: return
if (seen == sweepFed) return
sweepFed = seen
val facing = Sweep.headingAt(seen, t0, h0, now, h)
sweep.add(seen, facing, a.rssi)
+ if (sweepFrom == null) sweepFrom = pos
RadarTrack.reading(mac, seen, facing, a)
if (lock != null) return
val e = sweep.settled(seen) ?: return
@@ -251,7 +264,8 @@ object LiveRead {
/** The sweep's hint while locating: how to turn, then the locked direction. Null without a compass. Main thread. */
fun sweepText(now: Long = System.currentTimeMillis()): String? {
val h = heading ?: return null
- val l = held(now) ?: return "Phone on your chest, turn slowly: ${sweep.covered(now) * 360 / Sweep.SECTORS}° of 360°"
+ val l = held(now) ?: return if (compass?.upright != true) "Hold the phone upright on your chest"
+ else "Phone on your chest, turn round: ${sweep.covered(now) * 360 / Sweep.SECTORS}° of 360°"
val moved = sweepState(now)?.moved?.takeIf { it > SWEEP_STALE_M }
val far = locating?.let { RadarTrack.place(it, now) }?.let { " · ~" + DeviceIntel.formatDistance(it.distanceM.toDouble()) }.orEmpty()
return "${Sweep.compassPoint(l.bearing)}$far · ${turnText(Sweep.turn(h, l.bearing))} · " +
diff --git a/app/src/main/java/com/rfsentinel/app/ui/RadarTrack.kt b/app/src/main/java/com/rfsentinel/app/ui/RadarTrack.kt
index 39b6297..863c2c7 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/RadarTrack.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/RadarTrack.kt
@@ -41,6 +41,8 @@ object RadarTrack {
val readings = ArrayDeque<Reading>()
var radio = Radio(-59.0, 2.5)
var fed = 0L
+ /** Where this sweep's readings started: walking off starts it over. */
+ var sweepFrom: Motion.Pos? = null
var spot: Motion.Pos? = null
var solvedAt = 0L
var dirty = false
@@ -79,14 +81,24 @@ object RadarTrack {
t.dirty = true
}
+ /** The way you face now, for [mac]'s sweep (often, between readings too). */
+ fun face(mac: String, now: Long, heading: Float) {
+ val t = tracks[mac] ?: return
+ val pos = Motion.position
+ val from = t.sweepFrom
+ if (pos != null && from != null && hypot(pos.x - from.x, pos.y - from.y) > SWEEP_MOVE_M) { t.sweep.clear(); t.sweepFrom = null }
+ t.sweep.face(now, heading)
+ }
+
/** The radar's reading of an alert: kept, and run through its own sweep for a line. */
fun feed(mac: String, time: Long, heading: Float, a: Advert) {
if (!reading(mac, time, heading, a)) return
val t = tracks[mac] ?: return
t.sweep.add(time, heading, a.rssi)
+ if (t.sweepFrom == null) t.sweepFrom = Motion.position
val e = t.sweep.settled(time) ?: return
// The next turn gives a new, independent line.
- t.sweep.clear()
+ t.sweep.clear(); t.sweepFrom = null
line(mac, e.bearing, e.confidence, time)
}
@@ -180,6 +192,8 @@ object RadarTrack {
private const val SOLVE_MS = 2_000L
/** A turn within this of an earlier one is the same spot. */
private const val SAME_SPOT_M = 8.0
+ /** Walked this far mid-turn: that sweep starts over. */
+ private const val SWEEP_MOVE_M = 5.0
private const val CELL_M = 3.0
/** How far off a settled line usually is, and how much it counts against a reading. */
private const val SIGMA_DEG = 20.0
diff --git a/app/src/main/java/com/rfsentinel/app/ui/RadarView.kt b/app/src/main/java/com/rfsentinel/app/ui/RadarView.kt
index 5ba8312..a91c5a7 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/RadarView.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/RadarView.kt
@@ -82,6 +82,7 @@ class RadarView @JvmOverloads constructor(
// In a car its metal, not your body, shapes the signal.
if (fix != null && fix.hasSpeed() && fix.speed > WALK_MS) return
for (it in items) if (it.b.flagged) {
+ RadarTrack.face(it.b.mac, now, h)
val (seen, a) = DeviceRegistry.lastHeard(it.b.mac) ?: continue
RadarTrack.feed(it.b.mac, seen, Sweep.headingAt(seen, t0, h0, now, h), a)
}
diff --git a/app/src/main/java/com/rfsentinel/app/ui/Sweep.kt b/app/src/main/java/com/rfsentinel/app/ui/Sweep.kt
index 01e3cc0..d43a84f 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/Sweep.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/Sweep.kt
@@ -26,13 +26,32 @@ class Sweep {
private class Reading(val time: Long, val heading: Float, val rssi: Int)
private val readings = ArrayDeque<Reading>()
+ /** How long you faced each sector since a reading was last heard there. */
+ private val silent = LongArray(SECTORS)
+ private var facedAt = 0L
+ private var facedSector = -1
+
/** A new signal reading [rssi] heard while facing [heading] (compass degrees). */
fun add(now: Long, heading: Float, rssi: Int) {
- readings.addLast(Reading(now, (heading % 360f + 360f) % 360f, rssi))
+ val h = (heading % 360f + 360f) % 360f
+ val i = sector(h)
+ readings.addLast(Reading(now, h, rssi))
+ silent[i] = 0L
+ // Each sector keeps its newest few: standing still facing one way doesn't push the others out.
+ if (readings.count { sector(it.heading) == i } > PER_SECTOR) readings.removeAt(readings.indexOfFirst { sector(it.heading) == i })
while (readings.isNotEmpty() && now - readings.first().time > WINDOW_MS) readings.removeFirst()
}
- fun clear() { readings.clear(); candidate = null }
+ /** The way you face now, often (between readings too): facing a way long with nothing heard is your body's shadow. */
+ fun face(now: Long, heading: Float) {
+ val i = sector((heading % 360f + 360f) % 360f)
+ if (i == facedSector && facedAt > 0) silent[i] += now - facedAt
+ facedSector = i; facedAt = now
+ }
+
+ fun clear() { readings.clear(); candidate = null; silent.fill(0L); facedAt = 0L; facedSector = -1 }
+
+ private fun sector(heading: Float) = (heading / SECTOR_DEG).toInt().coerceIn(0, SECTORS - 1)
/**
* Sectors of the turn covered in the last [WINDOW_MS] (out of [SECTORS]): those with a reading, and a single
@@ -45,12 +64,27 @@ class Sweep {
}
/**
- * The median signal (dBm) heard facing each sector, null where you haven't faced yet. Sector i is centred on
- * [centre]. The median, not the mean: one deep multipath fade or one lucky packet doesn't swing a sector.
+ * The signal (dBm) facing each sector, null where you haven't faced yet. Sector i is centred on [centre]. The
+ * median heard there (one deep multipath fade or one lucky packet doesn't swing it); a sector faced
+ * [SILENT_MS] with nothing heard counts as [SILENT_DB] under the weakest heard: your back is to it.
*/
fun sectors(now: Long): Array<Float?> {
+ val m = heard(now)
+ val weakest = m.filterNotNull().minOrNull() ?: return m
+ val quiet = silentMs()
+ return Array(SECTORS) { i -> m[i] ?: if (silent[i] >= quiet) weakest - SILENT_DB else null }
+ }
+
+ /** Silence counts after [SILENT_MS], or twice the device's usual gap between packets if it sends slowly. */
+ private fun silentMs(): Long {
+ if (readings.size < 3) return SILENT_MS
+ val gaps = readings.zipWithNext { a, b -> b.time - a.time }.sorted()
+ return maxOf(SILENT_MS, 2 * gaps[gaps.size / 2])
+ }
+
+ private fun heard(now: Long): Array<Float?> {
val by = Array(SECTORS) { ArrayList<Int>() }
- for (r in readings) if (now - r.time <= WINDOW_MS) by[(r.heading / SECTOR_DEG).toInt().coerceIn(0, SECTORS - 1)] += r.rssi
+ for (r in readings) if (now - r.time <= WINDOW_MS) by[sector(r.heading)] += r.rssi
return Array(SECTORS) { i -> by[i].takeIf { it.isNotEmpty() }?.sorted()?.let { (it[(it.size - 1) / 2] + it[it.size / 2]) / 2f } }
}
@@ -63,13 +97,16 @@ class Sweep {
val m = sectors(now)
val k = m.indices.filter { m[it] != null }
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.
+ if (covered < MIN_SECTORS || heard(now).count { it != null } < MIN_HEARD) return null
+ // Normal equations for m = c + a cos t + b sin t, the stronger side weighing more: facing the device the
+ // signal is clean, behind you it's lost packets and reflections.
+ val lo = k.minOf { m[it]!! }; val span = (k.maxOf { m[it]!! } - lo).coerceAtLeast(1f)
val n = Array(3) { DoubleArray(4) }
for (i in k) {
val t = Math.toRadians(centre(i).toDouble())
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]!! }
+ val w = 1.0 + FRONT_WEIGHT * (m[i]!! - lo) / span
+ for (r in 0..2) { for (c in 0..2) n[r][c] += w * f[r] * f[c]; n[r][3] += w * f[r] * m[i]!! }
}
val (mean, a, b) = solve3(n) ?: return null
val amp = hypot(a, b)
@@ -83,7 +120,7 @@ class Sweep {
// reflection or a fade, so the rise and fall it fitted is less sure.
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 nullFactor = if (behind != null && behind <= weakest + NULL_TOLERANCE_DB) 1f else 0.75f
val confidence = covered.toFloat() / SECTORS * (amp / (amp + rest + NOISE_DB)).toFloat() * nullFactor
return Estimate(bearing, confidence.coerceIn(0f, 1f), (2 * amp).toFloat())
}
@@ -123,8 +160,14 @@ class Sweep {
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
+ /** Readings older than this drop out (walking somewhere new clears the sweep too: [LiveRead], [RadarTrack]). */
+ const val WINDOW_MS = 120_000L
+ private const val PER_SECTOR = 6
+ /** Faced this long with nothing heard (at least; see [silentMs]): the shadow, this much under the weakest heard. */
+ const val SILENT_MS = 1_500L
+ private const val SILENT_DB = 3f
+ /** The strongest sector weighs this much more than the weakest in the fit. */
+ private const val FRONT_WEIGHT = 2.0
/** Locked once this sure, and the body shadow shows by at least [LOCK_MIN_DB]. */
const val LOCK_CONFIDENCE = 0.6f
const val LOCK_MIN_DB = 4f
diff --git a/app/src/main/java/com/rfsentinel/app/ui/SweepGuide.kt b/app/src/main/java/com/rfsentinel/app/ui/SweepGuide.kt
index 91ac048..d3fbfe3 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/SweepGuide.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/SweepGuide.kt
@@ -110,7 +110,11 @@ class SweepGuide(private val activity: Activity) {
}
Step.SWEEP -> {
label.text = "2 · Sweeping"
- title.text = if (st.covered >= Sweep.MIN_SECTORS) "Once more round, a little slower" else "Keep turning"
+ title.text = when {
+ !st.upright -> "Hold it upright on your chest"
+ st.covered >= Sweep.MIN_SECTORS -> "Once more round, a little slower"
+ else -> "Keep turning"
+ }
val rssi = LiveRead.current()?.rssi
detail.text = (rssi?.let { "$it dBm · " } ?: "Not heard right now · ") +
"the ring fills with the signal heard each way, warmer = stronger" +
diff --git a/app/src/test/java/com/rfsentinel/app/ui/SweepTest.kt b/app/src/test/java/com/rfsentinel/app/ui/SweepTest.kt
index e333bf0..54385dd 100644
--- a/app/src/test/java/com/rfsentinel/app/ui/SweepTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/ui/SweepTest.kt
@@ -54,6 +54,26 @@ class SweepTest {
assertTrue(e.toString(), Sweep.locks(e))
}
+ @Test
+ fun facingAwayWithNothingHeardCountsAsTheShadow() {
+ val s = Sweep()
+ // Heard every 200 ms facing 0..270°, nothing at all with your back to it (300..360°).
+ for (h in 0 until 270 step 3) { s.add(1_000L + h * 20, h.toFloat(), (-70 + 6 * cos(Math.toRadians(h - 120.0))).toInt()) }
+ assertNull(s.sectors(7_000L)[10])
+ for (t in 0..40) s.face(7_000L + t * 50, 315f) // 2 s facing 315° with nothing heard
+ val weak = s.sectors(9_000L)[10]!!
+ assertTrue("$weak", weak <= s.sectors(9_000L).filterNotNull().min())
+ }
+
+ @Test
+ fun standingStillKeepsTheOtherSides() {
+ val s = Sweep()
+ for (h in 0 until 360 step 10) s.add(1_000L + h * 10, h.toFloat(), -70)
+ // A minute facing one way, heard all along: the rest of the turn stays.
+ for (t in 0 until 300) s.add(5_000L + t * 200, 180f, -75)
+ assertEquals(12, s.covered(65_000L))
+ }
+
@Test
fun locksOnlyOnceItHolds() {
val s = Sweep()