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.

Improve detection range and signal readings

CIS-C0eecf728632f7
diff --git a/app/src/main/java/com/rfsentinel/app/detect/DeviceIntel.kt b/app/src/main/java/com/rfsentinel/app/detect/DeviceIntel.kt
index e21a31f..5c3444d 100644
--- a/app/src/main/java/com/rfsentinel/app/detect/DeviceIntel.kt
+++ b/app/src/main/java/com/rfsentinel/app/detect/DeviceIntel.kt
@@ -303,14 +303,14 @@ object DeviceIntel {
* else a typical -59 dBm at 1 m. WiFi: free-space loss assuming a 20 dBm AP.
* Walls, bodies and antenna orientation easily cause 2-3x errors.
*/
- fun distanceMeters(a: Advert): Double {
+ fun distanceMeters(a: Advert, rssi: Double = a.rssi.toDouble()): Double {
if (a.isWifi) {
val f = a.wifi?.frequencyMhz?.takeIf { it > 0 } ?: 2437
- val exp = (20.0 - a.rssi - 20 * log10(f.toDouble()) + 27.55) / 20.0
+ val exp = (20.0 - rssi - 20 * log10(f.toDouble()) + 27.55) / 20.0
return 10.0.pow(exp).coerceIn(0.5, 500.0)
}
val measuredAt1m = a.txPower?.let { it - 41 } ?: -59
- return 10.0.pow((measuredAt1m - a.rssi) / 25.0).coerceIn(0.1, 300.0)
+ return 10.0.pow((measuredAt1m - rssi) / 25.0).coerceIn(0.1, 300.0)
}
fun formatDistance(m: Double): String = when {
diff --git a/app/src/main/java/com/rfsentinel/app/service/BleScanEngine.kt b/app/src/main/java/com/rfsentinel/app/service/BleScanEngine.kt
index 2c2ae8b..053feec 100644
--- a/app/src/main/java/com/rfsentinel/app/service/BleScanEngine.kt
+++ b/app/src/main/java/com/rfsentinel/app/service/BleScanEngine.kt
@@ -68,9 +68,18 @@ class BleScanEngine(
// Extended advertising (BT5) carries Remote ID message packs on newer drones.
val extended = runCatching { adapter.isLeExtendedAdvertisingSupported }.getOrDefault(false)
- fun settings(mode: Int) = ScanSettings.Builder().setScanMode(mode).apply {
- if (extended) setLegacy(false).setPhy(ScanSettings.PHY_LE_ALL_SUPPORTED)
- }.build()
+ // Deliver every advert immediately (no batching). For the filtered scan, aggressive
+ // matching reports a device from a single, even faint, advert instead of waiting
+ // for several strong ones - that's what extends screen-off detection range.
+ fun settings(mode: Int) = ScanSettings.Builder().setScanMode(mode)
+ .setReportDelay(0)
+ .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
+ .setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE)
+ .setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
+ .apply {
+ // Extended advertising (BT5) and LE Coded (long-range) PHY where the radio supports it.
+ if (extended) setLegacy(false).setPhy(ScanSettings.PHY_LE_ALL_SUPPORTED)
+ }.build()
try {
scanner?.startScan(null, settings(scanMode), callback)
@@ -84,7 +93,9 @@ class BleScanEngine(
return
}
try {
- scanner?.startScan(buildFilters(watchedMacs), settings(ScanSettings.SCAN_MODE_BALANCED), filteredCallback)
+ // Same intensity as the main scan: this is the only scan Android keeps running with
+ // the screen off, so a lower duty cycle here is where passing devices get missed.
+ scanner?.startScan(buildFilters(watchedMacs), settings(scanMode), filteredCallback)
filteredScanning = true
} catch (e: Exception) {
Log.w(TAG, "Filtered scan unavailable", e)
@@ -121,11 +132,13 @@ class BleScanEngine(
for (short in intArrayOf(0xFFFA, 0xFEAA, 0xFD5A, 0xFEED)) {
out += ScanFilter.Builder().setServiceData(ParcelUuid(Advert.uuid16(short)), ByteArray(0)).build()
}
- // Advertised services: Axon/TASER UUIDs and the Flock Raven GPS service.
- for (short in intArrayOf(0xFC81, 0xFE6B, 0xFE6C, 0x3100)) {
+ // Advertised services: Axon/TASER, Flock Raven GPS, Motorola Solutions, Zebra printers,
+ // Snap Spectacles and Meta glasses.
+ for (short in intArrayOf(0xFC81, 0xFE6B, 0xFE6C, 0x3100, 0xFD8E, 0xFE04, 0xFE79, 0xFD66, 0xFE45, 0xFEB7, 0xFEB8)) {
out += ScanFilter.Builder().setServiceUuid(ParcelUuid(Advert.uuid16(short))).build()
}
- watchedMacs.take(10).forEach { mac ->
+ // Controllers offload a limited number of filters; keep the total around 30.
+ watchedMacs.take(8).forEach { mac ->
runCatching { out += ScanFilter.Builder().setDeviceAddress(mac).build() }
}
return out
diff --git a/app/src/main/java/com/rfsentinel/app/service/DeviceRegistry.kt b/app/src/main/java/com/rfsentinel/app/service/DeviceRegistry.kt
index 4f9b0b1..ca056a5 100644
--- a/app/src/main/java/com/rfsentinel/app/service/DeviceRegistry.kt
+++ b/app/src/main/java/com/rfsentinel/app/service/DeviceRegistry.kt
@@ -14,6 +14,7 @@ import java.util.ArrayDeque
import java.util.concurrent.ConcurrentHashMap
import kotlin.math.atan2
import kotlin.math.cos
+import kotlin.math.roundToInt
import kotlin.math.sin
import kotlin.math.sqrt
@@ -35,6 +36,9 @@ object DeviceRegistry {
private const val ROTATION_SILENCE_MS = 1_500L
/** How long a device keeps a match after the evidence was last observed. */
const val HIT_HOLD_MS = 120_000L
+ /** Weight of each new RSSI reading (0..1): 0.3 settles in about a second of adverts. */
+ private const val RSSI_SMOOTHING = 0.3
+ private const val SMOOTH_RESET_MS = 10_000L
private const val ROTATION_WINDOW_MS = 30_000L
data class Sample(val time: Long, val rssi: Int)
@@ -84,6 +88,8 @@ object DeviceRegistry {
var identity = DeviceIntel.Identity("Device", emptyList())
var hits: List<Hit> = emptyList()
var rssi = -127
+ /** Exponentially smoothed RSSI; raw readings jump by 10 dB between packets. */
+ var smoothRssi = Double.NaN
var bestRssi = -127
var distance = 0.0
val firstSeen = now
@@ -142,10 +148,13 @@ object DeviceRegistry {
if (identity.facts.size >= t.identity.facts.size || t.identity.type == "Device") t.identity = identity
t.hits = mergeHits(t, hits, now)
remoteId?.let { t.remoteId = it }
- t.rssi = a.rssi
+ // Smooth for the displayed signal, distance and radar; a jump after a pause is taken as is.
+ t.smoothRssi = if (t.smoothRssi.isNaN() || now - t.lastSeen > SMOOTH_RESET_MS) a.rssi.toDouble()
+ else t.smoothRssi + RSSI_SMOOTHING * (a.rssi - t.smoothRssi)
+ t.rssi = t.smoothRssi.roundToInt()
if (location != null && (a.rssi >= t.bestRssi || t.bestPosition == null)) t.bestPosition = location
if (a.rssi > t.bestRssi) t.bestRssi = a.rssi
- t.distance = DeviceIntel.distanceMeters(a)
+ t.distance = DeviceIntel.distanceMeters(a, t.smoothRssi)
t.lastSeen = now
t.sightings++
if (t.history.isEmpty() || now - t.history.last.time >= HISTORY_MIN_SPACING_MS) {
diff --git a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
index 9d90b10..5c5ec67 100644
--- a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
+++ b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
@@ -92,6 +92,11 @@ class SettingsActivity : AppCompatActivity() {
}
)
binding.intervalInput.setText((Prefs.scanIntervalMs(this) / 1000).toString())
+ binding.wifiThrottleText.text = if (wifiScanThrottled())
+ "Android limits apps to 4 WiFi scans per 2 minutes, so values under 30 s are wasted. " +
+ "To scan faster: Developer options → turn off \"Wi-Fi scan throttling\"."
+ else
+ "Wi-Fi scan throttling is off (Developer options): intervals down to 5 s work, at some battery cost."
binding.bootSwitch.isChecked = Prefs.autoStartOnBoot(this)
binding.batteryButton.setOnClickListener {
runCatching { startActivity(Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)) }
@@ -157,6 +162,10 @@ class SettingsActivity : AppCompatActivity() {
binding.saveButton.setOnClickListener { save() }
}
+ /** Android's WiFi scan throttle (Developer options), on unless the user disabled it. */
+ private fun wifiScanThrottled(): Boolean =
+ runCatching { Settings.Global.getInt(contentResolver, "wifi_scan_throttle_enabled", 1) != 0 }.getOrDefault(true)
+
private fun confirm(title: String, message: String, action: () -> Unit) {
AlertDialog.Builder(this)
.setTitle(title)
@@ -178,7 +187,7 @@ class SettingsActivity : AppCompatActivity() {
}
)
val intervalSec = binding.intervalInput.text.toString().toLongOrNull() ?: 30L
- Prefs.setScanIntervalMs(this, intervalSec.coerceAtLeast(3) * 1000L)
+ Prefs.setScanIntervalMs(this, intervalSec.coerceAtLeast(5) * 1000L)
Prefs.setAutoStartOnBoot(this, binding.bootSwitch.isChecked)
categorySwitches.forEach { (c, sw) -> Prefs.setCategoryEnabled(this, c, sw.isChecked) }
diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml
index ec3f3c1..7bae308 100644
--- a/app/src/main/res/layout/activity_settings.xml
+++ b/app/src/main/res/layout/activity_settings.xml
@@ -102,6 +102,12 @@
android:importantForAutofill="no"
android:inputType="number" />
+ <TextView
+ android:id="@+id/wifiThrottleText"
+ style="@style/SettingsLabel"
+ android:paddingTop="2dp"
+ android:textSize="12sp" />
+
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/bootSwitch"
android:layout_width="match_parent"
diff --git a/app/src/test/java/com/rfsentinel/app/detect/IdentificationTest.kt b/app/src/test/java/com/rfsentinel/app/detect/IdentificationTest.kt
index 46b1613..4dfa77c 100644
--- a/app/src/test/java/com/rfsentinel/app/detect/IdentificationTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/detect/IdentificationTest.kt
@@ -139,6 +139,20 @@ class IdentificationTest {
assertNull(DeviceRegistry.get(mac)!!.best)
}
+ @Test
+ fun signalIsSmoothedButResetsAfterAPause() {
+ DeviceRegistry.startSession(0)
+ val mac = "C0:00:00:00:00:77"
+ val id = DeviceIntel.Identity("x", emptyList())
+ fun at(t: Long, rssi: Int) =
+ DeviceRegistry.report(Advert(mac, Advert.Source.BLE, rssi, null, timestamp = t), emptyList(), id, null, null, null, t)
+ at(1_000, -60)
+ at(1_100, -80) // one outlier packet moves the reading, but only part of the way
+ assertEquals(-66, DeviceRegistry.get(mac)!!.rssi)
+ at(60_000, -90) // heard again after a long gap: take the new value as is
+ assertEquals(-90, DeviceRegistry.get(mac)!!.rssi)
+ }
+
@Test
fun flatSignalsFallBackToArrivalTime() {
val flat = (0 until 30).map { it * 1000L to -60 }