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
OUI-SPY: choose its detection engines (Flock off in the EU); option to let the board do the Bluetooth scanning while it's live
CIS-C00f0eebb29e15
diff --git a/app/src/main/java/com/rfsentinel/app/esp/OuiSpyBle.kt b/app/src/main/java/com/rfsentinel/app/esp/OuiSpyBle.ktindex ae5392e..81d0f1e 100644--- a/app/src/main/java/com/rfsentinel/app/esp/OuiSpyBle.kt+++ b/app/src/main/java/com/rfsentinel/app/esp/OuiSpyBle.kt@@ -44,6 +44,8 @@ object OuiSpyBle {private var appContext: Context? = nullprivate var address: String? = nullprivate var relayAll = true+ /** The detection engines to switch on (Settings > OUI-SPY engines). */+ private var engines: List<Int> = OuiSpyBleProtocol.ENGINESprivate var onSightings: ((List<EspSighting>) -> Unit)? = null@Volatile private var active = false@@ -51,7 +53,11 @@ object OuiSpyBle {val connected: Boolean get() = activeprivate var inSpool = falseprivate var discovering = false- private var live = false+ /** Set up and reporting. */+ @Volatile var live = false; private set+ /** Told when [live] changes (the scan hands the phone's Bluetooth scan to the board, or takes it back). */+ var onLiveChanged: ((Boolean) -> Unit)? = null+ private fun setLive(v: Boolean) { if (live != v) { live = v; onLiveChanged?.invoke(v) } }private var closingUntil = 0Lprivate var detections = 0/** GATT writes must be sent one at a time. */@@ -101,13 +107,15 @@ object OuiSpyBle {}/** Starts (or keeps) the link to the board at [boardAddress] while scanning. */- fun start(context: Context, boardAddress: String, relayEverything: Boolean, sightings: (List<EspSighting>) -> Unit) = main.post {+ fun start(context: Context, boardAddress: String, relayEverything: Boolean, chosen: List<Int> = OuiSpyBleProtocol.ENGINES,+ sightings: (List<EspSighting>) -> Unit) = main.post {onSightings = sightings- if (active && address == boardAddress && relayAll == relayEverything) return@post+ if (active && address == boardAddress && relayAll == relayEverything && engines == chosen) return@poststopNow()appContext = context.applicationContextaddress = boardAddressrelayAll = relayEverything+ engines = chosenactive = trueconnect()}@@ -117,6 +125,7 @@ object OuiSpyBle {@SuppressLint("MissingPermission") // checked by canConnect()private fun stopNow() {active = false+ setLive(false)main.removeCallbacksAndMessages(null)val g = gatt ?: return// Switch our engines off so the board goes quiet, then let go.@@ -173,10 +182,14 @@ object OuiSpyBle {@SuppressLint("MissingPermission")private fun writeNext(g: BluetoothGatt) {val next = pending.removeFirstOrNull() ?: run {- live = true+ setLive(true)Log.i(TAG, "live")- setStatus(if (relayAll) "OUI-SPY · live · relaying everything it hears"- else "OUI-SPY · live · Flock, Sky Spy and Detector engines on")+ val names = engines.mapNotNull { OuiSpyBleProtocol.ENGINE_NAMES[it] }+ setStatus(when {+ relayAll -> "OUI-SPY · live · relaying everything it hears"+ names.isEmpty() -> "OUI-SPY · live · every engine off"+ else -> "OUI-SPY · live · ${names.joinToString()} on"+ })return}val ctl = g.getService(OuiSpyBleProtocol.SERVICE)?.getCharacteristic(OuiSpyBleProtocol.ENGINE_CONTROL) ?: return@@ -210,7 +223,7 @@ object OuiSpyBle {if (g !== gatt) return@postif (newState == BluetoothProfile.STATE_CONNECTED) {setStatus("OUI-SPY: connected, setting up…")- live = false+ setLive(false)discovering = false// On a reused link Android may never answer the MTU request: go on anyway.if (!g.requestMtu(MTU)) discover(g)@@ -228,6 +241,7 @@ object OuiSpyBle {} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {runCatching { g.close() }gatt = null+ setLive(false)if (active) { setStatus("OUI-SPY: out of reach - reconnecting…"); retry() }}}@@ -267,7 +281,9 @@ object OuiSpyBle {if (g !== gatt) return@postinSpool = falsepending.clear()- OuiSpyBleProtocol.engines(relayAll).forEach { pending.addLast(OuiSpyBleProtocol.enable(it)) }+ // Engines turned off in Settings go off on the board too (its own app may have left them on).+ (OuiSpyBleProtocol.ENGINES - engines.toSet()).forEach { pending.addLast(OuiSpyBleProtocol.disable(it)) }+ OuiSpyBleProtocol.engines(relayAll, engines).forEach { pending.addLast(OuiSpyBleProtocol.enable(it)) }writeNext(g)}}diff --git a/app/src/main/java/com/rfsentinel/app/esp/OuiSpyBleProtocol.kt b/app/src/main/java/com/rfsentinel/app/esp/OuiSpyBleProtocol.ktindex 09aad1a..ec8ae97 100644--- a/app/src/main/java/com/rfsentinel/app/esp/OuiSpyBleProtocol.kt+++ b/app/src/main/java/com/rfsentinel/app/esp/OuiSpyBleProtocol.kt@@ -37,8 +37,12 @@ object OuiSpyBleProtocol {/** Detection engines; RF Sentinel never touches the others (UniPwn, PCAP, Foxhunter). */val ENGINES = listOf(ENGINE_FLOCK_BLE, ENGINE_FLOCK_WIFI, ENGINE_SKYSPY, ENGINE_DETECTOR)- /** Engines to switch on; [relayAll] adds the survey engine so RF Sentinel's own rules see everything. */- fun engines(relayAll: Boolean) = if (relayAll) ENGINES + ENGINE_WARDRIVE else ENGINES+ /** The detection engines by name, for Settings and the status line. */+ val ENGINE_NAMES = linkedMapOf(ENGINE_FLOCK_BLE to "Flock BLE", ENGINE_FLOCK_WIFI to "Flock WiFi",+ ENGINE_SKYSPY to "Sky Spy", ENGINE_DETECTOR to "Detector")++ /** Engines to switch on: [chosen] of [ENGINES]; [relayAll] adds the survey engine so RF Sentinel's own rules see everything. */+ fun engines(relayAll: Boolean, chosen: List<Int> = ENGINES) = if (relayAll) chosen + ENGINE_WARDRIVE else chosenfun enable(engine: Int): ByteArray = byteArrayOf(0x01, engine.toByte())fun disable(engine: Int): ByteArray = byteArrayOf(0x00, engine.toByte())diff --git a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.ktindex bfee8d2..c1f9774 100644--- a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt+++ b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt@@ -364,9 +364,7 @@ class ScanForegroundService : Service() {// Re-evaluated on every start command, so Settings changes apply live - but the scan is only restarted when// its settings changed: Android silently stops delivering to an app that starts more than 5 scans in 30 s// (each start here is two), and a car start, Android Auto and Record trace can each send a start command.- if (Prefs.bleEnabled(this)) {- if (!bleEngine.isScanning() || bleSettingsKey() != bleStartedWith) { bleEngine.stop(); startBle() }- } else stopBle()+ applyBle()if (Prefs.wifiEnabled(this)) startWifiPolling() else stopWifiPolling()if (Prefs.categoryEnabled(this, Category.DRONE) && NanRemoteId.supported(this) && nearbyWifiAllowed()) {if (nanRemoteId == null) nanRemoteId = NanRemoteId(this) { a -> pipeline.post { processPhone(a) } }.also { it.start() }@@ -392,7 +390,10 @@ class ScanForegroundService : Service() {else com.rfsentinel.app.sdr.SdrRadio.stop(this)// An OUI-SPY board paired over Bluetooth (App-Controlled firmware).Prefs.ouiSpyBoard(this)?.let { addr ->- com.rfsentinel.app.esp.OuiSpyBle.start(this, addr, Prefs.ouiSpyRelayAll(this)) { list -> pipeline.post { list.forEach(::processEsp) } }+ com.rfsentinel.app.esp.OuiSpyBle.onLiveChanged = { serviceScope.launch { applyBle() } }+ com.rfsentinel.app.esp.OuiSpyBle.start(this, addr, Prefs.ouiSpyRelayAll(this), Prefs.ouiSpyEngines(this)) { list ->+ pipeline.post { list.forEach(::processEsp) }+ }} ?: com.rfsentinel.app.esp.OuiSpyBle.stop()updateLink()updateLocationUpdates()@@ -481,6 +482,17 @@ class ScanForegroundService : Service() {/** [bleSettingsKey] of the running scan. */private var bleStartedWith = ""+ /**+ * The phone's Bluetooth scan on or off as Settings say. With "Phone Bluetooth scan with OUI-SPY" off it pauses+ * while the OUI-SPY board is live (the board hears farther) and comes back as soon as the board drops out.+ */+ private fun applyBle() {+ val toBoard = !Prefs.ouiSpyPhoneBle(this) && Prefs.ouiSpyBoard(this) != null && com.rfsentinel.app.esp.OuiSpyBle.live+ if (Prefs.bleEnabled(this) && !toBoard) {+ if (!bleEngine.isScanning() || bleSettingsKey() != bleStartedWith) { bleEngine.stop(); startBle() }+ } else stopBle()+ }+private fun startBle() {bleStartedAt = System.currentTimeMillis()val watched = bleWatched()diff --git a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.ktindex 37f07aa..2c70b78 100644--- a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt+++ b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt@@ -358,6 +358,23 @@ class SettingsActivity : AppCompatActivity() {}}+ /** Settings > OUI-SPY engines: which of the board's detection engines run (applied to a running scan right away). */+ private fun chooseOuiSpyEngines() {+ val all = com.rfsentinel.app.esp.OuiSpyBleProtocol.ENGINE_NAMES+ val ids = all.keys.toList()+ val on = Prefs.ouiSpyEngines(this)+ val checked = BooleanArray(ids.size) { ids[it] in on }+ MaterialAlertDialogBuilder(this)+ .setTitle("OUI-SPY engines")+ .setMultiChoiceItems(all.values.toTypedArray(), checked) { _, i, v -> checked[i] = v }+ .setPositiveButton("OK") { _, _ ->+ Prefs.setOuiSpyEngines(this, ids.filterIndexed { i, _ -> checked[i] })+ if (com.rfsentinel.app.service.ScanForegroundService.isRunning) com.rfsentinel.app.service.ScanForegroundService.start(this)+ }+ .setNegativeButton("Cancel", null)+ .show()+ }+private class Board(val mac: String, val name: String, val rssi: Int)private fun showOuiSpyBoards(found: List<com.rfsentinel.app.esp.OuiSpyBle.Found>) {@@ -836,6 +853,12 @@ class SettingsActivity : AppCompatActivity() {info(binding.disable2gLabel, "2G doesn't check that a tower is real. Turning it off is the best protection against fake cell towers (IMSI catchers).")info(binding.cellSecurityLabel, "Android 15 and newer can warn you when a network asks for your phone's identity or turns encryption off.")info(binding.ouiSpyRelaySwitch, "Every device the OUI-SPY board hears comes into the list, not just its own matches.")+ info(binding.ouiSpyEnginesButton, "Which of the board's detection engines run: Flock BLE and Flock WiFi (Flock Safety " ++ "cameras), Sky Spy (drones) and Detector. Where there are no Flock cameras, turn the two Flock engines off. " ++ "RF Sentinel's own checks are set under Categories.")+ info(binding.ouiSpyPhoneBleSwitch, "Off: while the OUI-SPY board is connected and live, the phone's own Bluetooth scan " ++ "pauses and the board does the Bluetooth scanning (it hears farther, and faster). It comes back on its own if the " ++ "board drops out. Turn on Relay everything too, or only the board's engine matches come in.")info(binding.rtl5gSwitch, "For RTL8811AU / RTL8821AU adapters such as the ALFA AWUS036ACS. Experimental.")info(binding.usbLightSwitch, "For RTL8811AU / RTL8821AU adapters such as the ALFA AWUS036ACS: the light blinks with received traffic.")info(binding.pinpointSwitch, "Works out where a flagged device is from many readings as you move (GPS runs precise every second while one is in range).")@@ -2225,6 +2248,7 @@ class SettingsActivity : AppCompatActivity() {com.rfsentinel.app.esp.EspBoards.onStatusChanged = { runOnUiThread { updateEspStatus() } }com.rfsentinel.app.esp.OuiSpyBle.onStatusChanged = { runOnUiThread { updateEspStatus() } }binding.ouiSpyButton.setOnClickListener { pairOuiSpy() }+ binding.ouiSpyEnginesButton.setOnClickListener { chooseOuiSpyEngines() }binding.linkButton.setOnClickListener { startActivity(Intent(this, com.rfsentinel.app.ui.SentinelLinkActivity::class.java)) }binding.usbWifiLogButton.setOnClickListener { exportUsbWifiLog() }// FOSS build: an adapter waiting for its chip firmware asks to download it.@@ -2248,6 +2272,7 @@ class SettingsActivity : AppCompatActivity() {binding.pinpointSwitch.isChecked = Prefs.pinpointFlagged(this)binding.autoRecordSwitch.isChecked = Prefs.autoRecordTrace(this)binding.ouiSpyRelaySwitch.isChecked = Prefs.ouiSpyRelayAll(this)+ binding.ouiSpyPhoneBleSwitch.isChecked = Prefs.ouiSpyPhoneBle(this)binding.screenModeGroup.check(when (Prefs.screenMode(this)) {Prefs.ScreenMode.ALWAYS_ON -> R.id.screenAlwaysOnPrefs.ScreenMode.ON_WHILE_CHARGING -> R.id.screenOnCharging@@ -2512,6 +2537,7 @@ class SettingsActivity : AppCompatActivity() {Prefs.setPinpointFlagged(this, binding.pinpointSwitch.isChecked)Prefs.setAutoRecordTrace(this, binding.autoRecordSwitch.isChecked)Prefs.setOuiSpyRelayAll(this, binding.ouiSpyRelaySwitch.isChecked)+ Prefs.setOuiSpyPhoneBle(this, binding.ouiSpyPhoneBleSwitch.isChecked)Prefs.setScreenMode(this, when (binding.screenModeGroup.checkedRadioButtonId) {R.id.screenAlwaysOn -> Prefs.ScreenMode.ALWAYS_ONR.id.screenNormal -> Prefs.ScreenMode.NORMALdiff --git a/app/src/main/java/com/rfsentinel/app/util/Prefs.kt b/app/src/main/java/com/rfsentinel/app/util/Prefs.ktindex 305de3d..588e5fe 100644--- a/app/src/main/java/com/rfsentinel/app/util/Prefs.kt+++ b/app/src/main/java/com/rfsentinel/app/util/Prefs.kt@@ -404,6 +404,17 @@ object Prefs {fun setDeflockUpdated(context: Context, time: Long) = sp(context).edit { putLong("deflock_updated", time) }fun ouiSpyRelayAll(context: Context): Boolean = bool(context, "ouispy_relay_all", true)fun setOuiSpyRelayAll(context: Context, value: Boolean) = setBool(context, "ouispy_relay_all", value)+ /** The OUI-SPY detection engines to switch on (all by default; Flock off where there are no Flock cameras). */+ fun ouiSpyEngines(context: Context): List<Int> {+ val off = sp(context).getStringSet("ouispy_engines_off", emptySet()).orEmpty()+ return com.rfsentinel.app.esp.OuiSpyBleProtocol.ENGINES.filter { it.toString() !in off }+ }+ fun setOuiSpyEngines(context: Context, on: List<Int>) = sp(context).edit {+ putStringSet("ouispy_engines_off", com.rfsentinel.app.esp.OuiSpyBleProtocol.ENGINES.filter { it !in on }.map { it.toString() }.toSet())+ }+ /** Keep the phone's own Bluetooth scan running while the OUI-SPY board is live (off: the board scans alone). */+ fun ouiSpyPhoneBle(context: Context): Boolean = bool(context, "ouispy_phone_ble", true)+ fun setOuiSpyPhoneBle(context: Context, value: Boolean) = setBool(context, "ouispy_phone_ble", value)/** Two- or three-word spoken alerts ("Body cam", "Speed camera, 50") instead of full labels. */fun shortVoice(context: Context): Boolean = bool(context, "short_voice", false)fun setShortVoice(context: Context, value: Boolean) = setBool(context, "short_voice", value)diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xmlindex a204e08..c0e4cc0 100644--- a/app/src/main/res/layout/activity_settings.xml+++ b/app/src/main/res/layout/activity_settings.xml@@ -755,6 +755,19 @@android:layout_height="wrap_content"android:text="OUI-SPY: relay everything" />+ <Button+ android:id="@+id/ouiSpyEnginesButton"+ style="@style/Widget.MaterialComponents.Button.TextButton"+ android:layout_width="wrap_content"+ android:layout_height="wrap_content"+ android:text="OUI-SPY engines" />++ <com.google.android.material.switchmaterial.SwitchMaterial+ android:id="@+id/ouiSpyPhoneBleSwitch"+ android:layout_width="match_parent"+ android:layout_height="wrap_content"+ android:text="Phone Bluetooth scan with OUI-SPY" />+<com.google.android.material.switchmaterial.SwitchMaterialandroid:id="@+id/rtl5gSwitch"android:layout_width="match_parent"