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.

OUI-Spy over USB: quiet modes, Detector backfill, mode hints

CIS-C0950d1a4c0fc2
diff --git a/app/src/main/java/com/rfsentinel/app/esp/EspReader.kt b/app/src/main/java/com/rfsentinel/app/esp/EspReader.kt
index f7c068a..3b770ec 100644
--- a/app/src/main/java/com/rfsentinel/app/esp/EspReader.kt
+++ b/app/src/main/java/com/rfsentinel/app/esp/EspReader.kt
@@ -1,9 +1,10 @@
package com.rfsentinel.app.esp
/**
- * Reads one ESP32 board until [stop] is called. RF Sentinel only listens:
- * OUI-Spy streams its detections by itself and is never sent anything;
- * GhostESP is only ever asked to scan for networks and list them.
+ * Reads one ESP32 board until [stop] is called. RF Sentinel only listens and
+ * asks read-only questions: OUI-Spy may be sent `CMD:VERSION` and, in
+ * Detector mode, `CMD:DUMP_LIVE` (what it has found so far); GhostESP is only
+ * asked for `help`, to scan networks and to list them.
*/
class EspReader(
private val port: SerialPort,
@@ -17,44 +18,67 @@ class EspReader(
fun run() {
val buf = ByteArray(4096)
onStatus("ESP32 · listening…")
- val heard = StringBuilder()
- val listenUntil = System.currentTimeMillis() + 4_000
- while (!stopped && System.currentTimeMillis() < listenUntil && !OuiSpyReports.recognises(heard.toString())) {
- val n = port.read(buf, 300)
- if (n > 0) heard.append(String(buf, 0, n))
+ // 1. OUI-Spy prints on its own (banners, status, detections).
+ var heard = readFor(buf, 4_000) { OuiSpyReports.recognises(it) }
+ if (stopped) return
+ // 2. Detector, PCAP and BLE-sniff modes answer a version request.
+ if (!OuiSpyReports.recognises(heard)) {
+ port.write("CMD:VERSION\n")
+ heard += readFor(buf, 1_500) { OuiSpyReports.recognises(it) }
}
if (stopped) return
- if (OuiSpyReports.recognises(heard.toString())) {
- readOuiSpy(heard.toString(), buf)
- return
+ if (!OuiSpyReports.recognises(heard)) {
+ // 3. GhostESP answers `help` with its banner.
+ port.write("help\r\n")
+ val banner = readFor(buf, 2_500) { GhostEspReports.recognises(it) }
+ if (GhostEspReports.recognises(banner)) { pollGhostEsp(buf); return }
+ heard += banner
}
- // Not streaming on its own: see whether it's GhostESP.
- port.write("help\r\n")
- val banner = readFor(buf, 2_500)
- if (GhostEspReports.recognises(banner)) pollGhostEsp(buf)
- else onStatus("ESP32 connected, but its firmware isn't OUI-Spy or GhostESP")
+ // 4. OUI-Spy - or a quiet board: Flock-You and Sky Spy say nothing until they find
+ // something, so keep listening rather than giving up on it.
+ readOuiSpy(heard, buf)
}
private fun readOuiSpy(already: String, buf: ByteArray) {
- val pending = StringBuilder(already)
+ var mode = OuiSpyReports.modeOf(already)
+ val pending = StringBuilder()
var count = 0
- onStatus("OUI-Spy · live")
- while (!stopped) {
- val n = port.read(buf, 500)
- if (n <= 0) continue
- pending.append(String(buf, 0, n))
+ fun status() = onStatus(when {
+ mode == OuiSpyReports.Mode.SELECTOR ->
+ "OUI-Spy is in its mode selector - join its Wi-Fi \"oui-spy\", open 192.168.4.1 and pick Flock-You, Detector or Sky Spy"
+ mode == OuiSpyReports.Mode.OTHER ->
+ "This OUI-Spy mode isn't used by RF Sentinel - pick Flock-You, Detector or Sky Spy"
+ mode == null && count == 0 -> "ESP32 connected · waiting for detections (OUI-Spy reports only when it finds something)"
+ else -> "OUI-Spy ${mode?.label ?: ""} · live · $count detection${if (count == 1) "" else "s"}"
+ })
+ fun consume(text: String) {
+ pending.append(text)
while (true) {
val nl = pending.indexOf('\n')
if (nl < 0) break
val line = pending.substring(0, nl)
pending.delete(0, nl + 1)
+ OuiSpyReports.modeOf(line)?.let { if (it != mode) { mode = it; status() } }
OuiSpyReports.parse(line)?.let {
count++
onSightings(listOf(it))
- onStatus("OUI-Spy · live · $count detections")
+ status()
}
}
- if (pending.length > 16_384) pending.setLength(0) // not line-based output (another mode)
+ if (pending.length > 16_384) pending.setLength(0) // binary output (PCAP modes) isn't for us
+ }
+ consume(already) // what was heard while identifying (a partial last line stays pending)
+ status()
+ // Detector keeps what it found in RAM: fetch it once (read-only).
+ if (mode == OuiSpyReports.Mode.DETECTOR) port.write("CMD:DUMP_LIVE\n")
+ var askedDump = mode == OuiSpyReports.Mode.DETECTOR
+ while (!stopped) {
+ val n = port.read(buf, 500)
+ if (n > 0) consume(String(buf, 0, n))
+ if (!askedDump && mode == OuiSpyReports.Mode.DETECTOR) {
+ askedDump = true
+ port.write("CMD:DUMP_LIVE\n")
+ }
}
}
@@ -74,12 +98,16 @@ class EspReader(
runCatching { port.write("stopscan\r\n") }
}
- private fun readFor(buf: ByteArray, ms: Long): String {
+ /** Reads for up to [ms], stopping early once [done] is true for what was read. */
+ private fun readFor(buf: ByteArray, ms: Long, done: (String) -> Boolean = { false }): String {
val sb = StringBuilder()
val until = System.currentTimeMillis() + ms
while (!stopped && System.currentTimeMillis() < until) {
val n = port.read(buf, 300)
- if (n > 0) sb.append(String(buf, 0, n))
+ if (n > 0) {
+ sb.append(String(buf, 0, n))
+ if (done(sb.toString())) break
+ }
}
return sb.toString()
}
diff --git a/app/src/main/java/com/rfsentinel/app/esp/EspReports.kt b/app/src/main/java/com/rfsentinel/app/esp/EspReports.kt
index c2fe3cd..2806f7c 100644
--- a/app/src/main/java/com/rfsentinel/app/esp/EspReports.kt
+++ b/app/src/main/java/com/rfsentinel/app/esp/EspReports.kt
@@ -36,12 +36,31 @@ object OuiSpyReports {
fun recognises(text: String): Boolean =
"\"event\":\"detection\"" in text || "\"drone_lat\"" in text || "\"status\":\"scanning\"" in text ||
- "OUI-SPY" in text || "[flockyou]" in text
+ "OUI-SPY" in text || "[flockyou]" in text || "[SELECTOR]" in text
+
+ /** OUI-Spy modes, as far as RF Sentinel is concerned. */
+ enum class Mode(val label: String, val usable: Boolean) {
+ FLOCK_YOU("Flock-You", true), DETECTOR("Detector", true), SKY_SPY("Sky Spy", true),
+ SELECTOR("mode selector", false), OTHER("PCAP / BLE sniff / fox hunter", false)
+ }
+
+ /** Which mode [text] (banners, command replies or detection lines) shows, if any. */
+ fun modeOf(text: String): Mode? = when {
+ "\"drone_lat\"" in text || "\"status\":\"scanning\"" in text || "SKY SPY" in text -> Mode.SKY_SPY
+ "\"detection_tier\"" in text || "[flockyou]" in text || "FLOCK-YOU" in text -> Mode.FLOCK_YOU
+ "\"match_method\"" in text || "BLE DETECTOR" in text || "STARTING DETECTOR" in text ||
+ "\"mode\":\"ble_detector\"" in text -> Mode.DETECTOR
+ "[SELECTOR]" in text || "Firmware Selector" in text || "STARTING SELECTOR" in text -> Mode.SELECTOR
+ "PCAP" in text || "BLE SNIFF" in text || "FOXHUNT" in text || "blesniff" in text -> Mode.OTHER
+ else -> null
+ }
fun parse(line: String): EspSighting? {
val t = line.trim()
if (!t.startsWith("{") || !t.endsWith("}")) return null
val o = runCatching { JsonParser.parseString(t).asJsonObject }.getOrNull() ?: return null
+ // Detections from a previous session (replayed from the board's flash) are old news.
+ if (o.text("replay_source") == "flash") return null
return when {
o.has("drone_lat") -> drone(o)
o.text("event") == "detection" -> detection(o)
diff --git a/app/src/test/java/com/rfsentinel/app/esp/EspReaderTest.kt b/app/src/test/java/com/rfsentinel/app/esp/EspReaderTest.kt
new file mode 100644
index 0000000..d1e5855
--- /dev/null
+++ b/app/src/test/java/com/rfsentinel/app/esp/EspReaderTest.kt
@@ -0,0 +1,109 @@
+package com.rfsentinel.app.esp
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import java.util.concurrent.ConcurrentLinkedQueue
+import java.util.concurrent.CopyOnWriteArrayList
+
+/** Drives [EspReader] with a fake board that answers like each firmware does. */
+class EspReaderTest {
+
+ /** Emits [boot] at start, then replies to commands via [reply]. */
+ private class FakeBoard(boot: String, private val reply: (String) -> String?) : SerialPort {
+ val written = CopyOnWriteArrayList<String>()
+ private val out = ConcurrentLinkedQueue<ByteArray>().apply { if (boot.isNotEmpty()) add(boot.toByteArray()) }
+
+ fun push(text: String) { out.add(text.toByteArray()) }
+
+ override fun write(text: String) {
+ written += text.trim()
+ reply(text.trim())?.let { push(it) }
+ }
+
+ override fun read(buf: ByteArray, timeoutMs: Int): Int {
+ val b = out.poll() ?: run { Thread.sleep(minOf(timeoutMs, 20).toLong()); return 0 }
+ System.arraycopy(b, 0, buf, 0, b.size)
+ return b.size
+ }
+
+ override fun close() {}
+ }
+
+ private fun runFor(board: FakeBoard, ms: Long, afterStart: (FakeBoard) -> Unit = {}): Pair<List<String>, List<EspSighting>> {
+ val statuses = CopyOnWriteArrayList<String>()
+ val seen = CopyOnWriteArrayList<EspSighting>()
+ val r = EspReader(board, { statuses += it }) { seen += it }
+ val t = Thread { r.run() }
+ t.start()
+ afterStart(board)
+ Thread.sleep(ms)
+ r.stop()
+ t.join(3_000)
+ return statuses to seen
+ }
+
+ private val flockLine = """{"event":"detection","detection_method":"wifi_wildcard_probe","detection_tier":3,"protocol":"wifi_2_4ghz","mac_address":"70:c9:4e:11:22:33","oui":"70:c9:4e","device_name":"","rssi":-71,"channel":6,"frequency":2437,"ssid":""}""" + "\n"
+
+ @Test
+ fun streamingFlockYouIsReadWithoutSendingAnything() {
+ val board = FakeBoard(flockLine) { null }
+ val (statuses, seen) = runFor(board, 600)
+ assertEquals(1, seen.size)
+ assertTrue(board.written.isEmpty())
+ assertTrue(statuses.last().startsWith("OUI-Spy Flock-You · live · 1 detection"))
+ }
+
+ @Test
+ fun aQuietBoardIsKeptAndItsLaterDetectionsAreRead() {
+ // Flock-You with nothing around: silent, ignores CMD:VERSION and help.
+ val board = FakeBoard("") { null }
+ val (statuses, seen) = runFor(board, 9_500) { b ->
+ Thread { Thread.sleep(8_800); b.push(flockLine) }.start()
+ }
+ assertEquals(listOf("CMD:VERSION", "help"), board.written.toList())
+ assertTrue(statuses.any { it.startsWith("ESP32 connected · waiting for detections") })
+ assertEquals(1, seen.size)
+ }
+
+ @Test
+ fun detectorIsIdentifiedAndItsLiveTableFetched() {
+ val det = """{"event":"detection","protocol":"ble","detection_method":"ble_oui","mac_address":"00:25:df:aa:bb:cc","addr_type":"public","rssi":-55,"rssi_min":-70,"rssi_max":-50,"company_id":845,"service_uuid":null,"local_name":"","device_name":"","match_method":"oui","matched_signature":"00:25:DF","first_seen_ms":1,"last_seen_ms":2,"hit_count":3,"replay_source":"ram"}"""
+ val old = det.replace("\"ram\"", "\"flash\"").replace("aa:bb:cc", "00:00:01")
+ val board = FakeBoard("") { cmd ->
+ when (cmd) {
+ "CMD:VERSION" -> "OUI-SPY BLE DETECTOR 1.2 built Jan 1 2026\n"
+ "CMD:DUMP_LIVE" -> "BEGIN_DUMP live bytes=0 count=2\n$det\n$old\nEND_DUMP live count=2\n"
+ else -> null
+ }
+ }
+ val (statuses, seen) = runFor(board, 6_000)
+ assertEquals(listOf("CMD:VERSION", "CMD:DUMP_LIVE"), board.written.toList())
+ assertEquals(listOf("00:25:DF:AA:BB:CC"), seen.map { it.mac }) // the replayed old session is skipped
+ assertTrue(statuses.last().startsWith("OUI-Spy Detector · live"))
+ }
+
+ @Test
+ fun selectorModeTellsTheUserToPickAMode() {
+ val board = FakeBoard("[SELECTOR] *** SELECTOR FULLY INITIALIZED ***\n[SELECTOR] WiFi AP: 'oui-spy'\n") { null }
+ val (statuses, seen) = runFor(board, 600)
+ assertTrue(seen.isEmpty())
+ assertTrue(statuses.last().startsWith("OUI-Spy is in its mode selector"))
+ assertFalse(board.written.contains("help"))
+ }
+
+ @Test
+ fun ghostEspIsPolledForNetworks() {
+ val board = FakeBoard("") { cmd ->
+ when (cmd) {
+ "help" -> "Ghost ESP Commands:\n"
+ "list -a" -> "[0] SSID: Example Net, BSSID: 34:53:D2:C4:5D:E6, RSSI: -55, Company: Unknown\n"
+ else -> null
+ }
+ }
+ val (_, seen) = runFor(board, 16_500)
+ assertTrue(board.written.containsAll(listOf("help", "scanap", "stopscan", "list -a")))
+ assertTrue(seen.any { it.name == "Example Net" })
+ }
+}