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 pairing: search for the board's Bluetooth service instead of relying on the scan having heard its name

CIS-C0870a6bd8c26d
diff --git a/app/src/main/java/com/rfsentinel/app/esp/OuiSpyBle.kt b/app/src/main/java/com/rfsentinel/app/esp/OuiSpyBle.kt
index 9aff7ba..ae5392e 100644
--- a/app/src/main/java/com/rfsentinel/app/esp/OuiSpyBle.kt
+++ b/app/src/main/java/com/rfsentinel/app/esp/OuiSpyBle.kt
@@ -8,7 +8,12 @@ import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattDescriptor
import android.bluetooth.BluetoothManager
import android.bluetooth.BluetoothProfile
+import android.bluetooth.le.ScanCallback
+import android.bluetooth.le.ScanFilter
+import android.bluetooth.le.ScanResult
+import android.bluetooth.le.ScanSettings
import android.content.Context
+import android.os.ParcelUuid
import android.os.Build
import android.os.Handler
import android.os.Looper
@@ -61,6 +66,40 @@ object OuiSpyBle {
fun canConnect(context: Context) = Build.VERSION.SDK_INT < 31 ||
Permissions.granted(context, android.Manifest.permission.BLUETOOTH_CONNECT)
+ /** A board heard by [find]. */
+ class Found(val address: String, val name: String?, val rssi: Int)
+
+ private val finder = Handler(Looper.getMainLooper())
+
+ /**
+ * Looks for OUI-SPY boards nearby for [ms], whether or not RF Sentinel is scanning. Boards advertise the
+ * OUI-SPY service; their "OUI-SPY-xxxx" name is only in the scan response, which a busy scan can miss.
+ * A board connected to another phone app doesn't advertise and isn't found.
+ */
+ @SuppressLint("MissingPermission")
+ fun find(context: Context, ms: Long = 6_000L, done: (List<Found>) -> Unit) {
+ val scanner = runCatching {
+ context.getSystemService(BluetoothManager::class.java)?.adapter?.takeIf { it.isEnabled }?.bluetoothLeScanner
+ }.getOrNull()
+ if (scanner == null) { done(emptyList()); return }
+ val seen = LinkedHashMap<String, Found>()
+ val cb = object : ScanCallback() {
+ override fun onScanResult(callbackType: Int, r: ScanResult) {
+ val name = r.scanRecord?.deviceName ?: runCatching { r.device.name }.getOrNull()
+ val addr = r.device.address
+ finder.post { seen[addr] = Found(addr, name ?: seen[addr]?.name, r.rssi) }
+ }
+ }
+ val filters = listOf(ScanFilter.Builder().setServiceUuid(ParcelUuid(OuiSpyBleProtocol.SERVICE)).build())
+ // Legacy, active, fastest: the scan response with the name comes along.
+ val settings = ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(0).build()
+ try { scanner.startScan(filters, settings, cb) } catch (e: Exception) { Log.w(TAG, "OUI-SPY search failed", e); done(emptyList()); return }
+ finder.postDelayed({
+ runCatching { scanner.stopScan(cb) }
+ done(seen.values.sortedByDescending { it.rssi })
+ }, ms)
+ }
+
/** 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 {
onSightings = sightings
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 1b70f44..9db2948 100644
--- a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
+++ b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
@@ -327,25 +327,48 @@ class SettingsActivity : AppCompatActivity() {
) { granted -> if (granted) pairOuiSpy() }
/**
- * Picks the OUI-SPY board among nearby devices RF Sentinel hears (they
- * advertise as "OUI-SPY-xxxx"); scanning must be running to see it.
+ * Picks the OUI-SPY board among the boards nearby: a few seconds' search for the OUI-SPY service (scanning
+ * needn't be running), plus any board the scan already heard by name ("OUI-SPY-xxxx").
*/
private fun pairOuiSpy() {
if (!com.rfsentinel.app.esp.OuiSpyBle.canConnect(this)) {
ouiSpyConnectPermission.launch(android.Manifest.permission.BLUETOOTH_CONNECT); return
}
- val boards = com.rfsentinel.app.service.DeviceRegistry.snapshot()
+ val looking = AlertDialog.Builder(this)
+ .setTitle("Looking for OUI-SPY boards…")
+ .setMessage("Keep the board powered and close by.")
+ .setNegativeButton("Cancel", null)
+ .show()
+ com.rfsentinel.app.esp.OuiSpyBle.find(this) { found ->
+ if (!looking.isShowing || isFinishing || isDestroyed) return@find
+ looking.dismiss()
+ showOuiSpyBoards(found)
+ }
+ }
+
+ private class Board(val mac: String, val name: String, val rssi: Int)
+
+ private fun showOuiSpyBoards(found: List<com.rfsentinel.app.esp.OuiSpyBle.Found>) {
+ val heard = com.rfsentinel.app.service.DeviceRegistry.snapshot()
.filter { it.name?.startsWith(com.rfsentinel.app.esp.OuiSpyBleProtocol.NAME_PREFIX) == true }
+ .map { Board(it.mac.uppercase(), it.name!!, it.rssi) }
+ val boards = (found.map { Board(it.address.uppercase(), it.name ?: "OUI-SPY board", it.rssi) } + heard)
+ .distinctBy { it.mac }
.sortedByDescending { it.rssi }
val current = Prefs.ouiSpyBoard(this)
val labels = boards.map { "${it.name} (${it.mac}, ${it.rssi} dBm)" }.toMutableList()
if (current != null) labels += "Forget the paired board"
if (labels.isEmpty()) {
AlertDialog.Builder(this)
- .setTitle("No OUI-SPY board heard")
- .setMessage("Power the board (Bluetooth / App-Controlled firmware) and start scanning, " +
- "then come back here: it shows up as \"OUI-SPY-xxxx\".")
- .setPositiveButton("OK", null)
+ .setTitle("No OUI-SPY board found")
+ .setMessage("Check that:\n\n" +
+ "• the board runs the Bluetooth \"App-Controlled\" firmware and has power\n" +
+ "• the OUI-SPY app isn't connected to it: a board connected to a phone stops advertising. " +
+ "Force stop that app in Android's app settings, then unplug the board for a few seconds\n" +
+ "• it isn't paired in Android's Bluetooth settings: RF Sentinel connects to it directly\n" +
+ "• Bluetooth is on and RF Sentinel may use Nearby devices")
+ .setPositiveButton("Try again") { _, _ -> pairOuiSpy() }
+ .setNegativeButton("OK", null)
.show()
return
}