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.

P25: link devices that are around each time the same radio keys up nearby

CIS-C05914112573a8
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/P25Link.kt b/app/src/main/java/com/rfsentinel/app/sdr/P25Link.kt
new file mode 100644
index 0000000..f4ce42d
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/sdr/P25Link.kt
@@ -0,0 +1,54 @@
+package com.rfsentinel.app.sdr
+
+import com.rfsentinel.app.detect.Category
+import com.rfsentinel.app.detect.Hit
+
+/**
+ * Links a P25 radio heard keying up near you (its input, [SdrRadio.onP25Input]) with the Bluetooth / WiFi devices
+ * around each time. A device that is there on several separate occasions, and with that radio at least two thirds of
+ * the times it is near any radio, likely rides with it: the officer's phone, body cam or car router. Your own devices
+ * are around for every radio, so that rule leaves them out once a few other radios have been heard. Pure: unit-tested.
+ */
+object P25Link {
+
+ /** Separate occasions a device must share with a radio before it is linked. */
+ const val MIN_TIMES = 3
+ /** Keying up again within this is the same occasion. */
+ private const val SAME_TIME_MS = 10 * 60_000L
+ private const val CONFIDENCE = 60
+ const val SOURCE = "P25 radio near you, the same device around each time"
+
+ private class Shared(var times: Int = 0, var last: Long = Long.MIN_VALUE / 2)
+
+ /** (radio, device) → shared occasions; device → occasions with any radio. */
+ private val shared = HashMap<Int, HashMap<String, Shared>>()
+ private val anyRadio = HashMap<String, Int>()
+
+ /** Radio [radio] keyed up near you at [now] with [macs] in range. */
+ @Synchronized fun heard(radio: Int, macs: Collection<String>, now: Long) {
+ val withRadio = shared.getOrPut(radio) { HashMap() }
+ for (mac in macs) {
+ val p = withRadio.getOrPut(mac) { Shared() }
+ if (now - p.last < SAME_TIME_MS) { p.last = now; continue }
+ p.times++; p.last = now
+ anyRadio.merge(mac, 1, Int::plus)
+ }
+ }
+
+ /** The radio [mac] rides with, and the occasions they shared, or null. */
+ @Synchronized fun radioFor(mac: String): Pair<Int, Int>? {
+ val all = anyRadio[mac] ?: return null
+ return shared.entries.mapNotNull { (radio, m) -> m[mac]?.times?.let { radio to it } }
+ .filter { (_, times) -> times >= MIN_TIMES && times * 3 >= all * 2 } // at least 2 of every 3 times with radios
+ .maxByOrNull { it.second }
+ }
+
+ /** The link as a match on the device, for its classification. */
+ fun hitFor(mac: String): Hit? = radioFor(mac)?.let { (radio, times) ->
+ Hit(Category.PUBLIC_SAFETY, "Rides with P25 radio $radio", CONFIDENCE,
+ "P25 radio $radio keyed up near you $times separate times, and this device was around each time - " +
+ "more often than with any other radio. It may be that radio user's phone, body cam or vehicle.", SOURCE)
+ }
+
+ @Synchronized fun clear() { shared.clear(); anyRadio.clear() }
+}
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 d31ed21..c734c53 100644
--- a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
+++ b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
@@ -1051,7 +1051,7 @@ class ScanForegroundService : Service() {
OuiWatchlist.hits(a.mac, a.name, listOfNotNull(macVendor) + companyVendors) +
OuiWatchlist.advertHits(a) +
DeviceRegistry.inheritedHits(a.mac) +
- listOfNotNull(DeviceRegistry.clusterHit(a.mac, now)) +
+ listOfNotNull(DeviceRegistry.clusterHit(a.mac, now), com.rfsentinel.app.sdr.P25Link.hitFor(a.mac)) +
espHits[a.mac]?.takeIf { now - it.first < ESP_HIT_TTL_MS }?.second.orEmpty() +
probeHits[a.mac]?.takeIf { now - it.first < ESP_HIT_TTL_MS }?.second.orEmpty() +
extra
@@ -1562,6 +1562,11 @@ class ScanForegroundService : Service() {
private fun onP25Input(call: com.rfsentinel.app.sdr.P25History.Call, hz: Long, snr: Int) {
if (!Prefs.categoryEnabled(this, Category.RADIO)) return
val now = System.currentTimeMillis()
+ // The devices around right now (not your whitelisted ones): one that's here each time this radio is may ride with it.
+ call.source?.let { radio ->
+ com.rfsentinel.app.sdr.P25Link.heard(radio, DeviceRegistry.snapshot(now)
+ .filter { com.rfsentinel.app.ui.LiveWindow.inRange(it, now) && !WhitelistCache.contains(it.mac) }.map { it.mac }, now)
+ }
val min = Prefs.radioMinSnr(this)
val confidence = if (snr >= min + 10) 75 else 60
val name = call.tg?.let { com.rfsentinel.app.sdr.P25Log.watched(Prefs.p25Talkgroups(this))[it]?.ifEmpty { null } }
diff --git a/app/src/test/java/com/rfsentinel/app/sdr/P25LinkTest.kt b/app/src/test/java/com/rfsentinel/app/sdr/P25LinkTest.kt
new file mode 100644
index 0000000..31cabd7
--- /dev/null
+++ b/app/src/test/java/com/rfsentinel/app/sdr/P25LinkTest.kt
@@ -0,0 +1,28 @@
+package com.rfsentinel.app.sdr
+
+import org.junit.After
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNull
+import org.junit.Test
+
+class P25LinkTest {
+
+ @After fun tearDown() = P25Link.clear()
+
+ @Test
+ fun aDeviceAroundEachTimeARadioKeysUpIsLinked() {
+ val min = 60_000L
+ // Radio 5001 keys up near you three separate times (30 min apart); the body cam and your earbuds are there each time.
+ for (t in 0 until 3) P25Link.heard(5001, listOf("CAM", "EARBUDS"), t * 30 * min)
+ P25Link.heard(5001, listOf("CAM"), 2 * 30 * min + 2 * min) // the same occasion again: not a fourth
+ assertEquals(5001 to 3, P25Link.radioFor("CAM"))
+ // Your earbuds are also there for two other radios: not mostly with 5001, so no link.
+ P25Link.heard(6002, listOf("EARBUDS"), 200 * min)
+ P25Link.heard(7003, listOf("EARBUDS"), 300 * min)
+ assertNull(P25Link.radioFor("EARBUDS"))
+ assertEquals("Rides with P25 radio 5001", P25Link.hitFor("CAM")?.label)
+ // Twice isn't enough.
+ P25Link.heard(8004, listOf("PHONE"), 0); P25Link.heard(8004, listOf("PHONE"), 30 * min)
+ assertNull(P25Link.hitFor("PHONE"))
+ }
+}