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.

Android Auto: camera focus view; devices map mixes Bluetooth and WiFi

CIS-C0d10c7223cbd2
diff --git a/app/src/main/java/com/rfsentinel/app/car/CarUi.kt b/app/src/main/java/com/rfsentinel/app/car/CarUi.kt
index 0bf996b..983140a 100644
--- a/app/src/main/java/com/rfsentinel/app/car/CarUi.kt
+++ b/app/src/main/java/com/rfsentinel/app/car/CarUi.kt
@@ -38,6 +38,18 @@ object CarUi {
.maxByOrNull { it.time }
}
+ /** Hands a spot to the car's navigation app (Google Maps, Waze...). */
+ fun navigateTo(context: CarContext, lat: Double, lon: Double) {
+ try {
+ context.startCarApp(
+ android.content.Intent(CarContext.ACTION_NAVIGATE,
+ android.net.Uri.parse(String.format(java.util.Locale.US, "geo:%.7f,%.7f", lat, lon)))
+ )
+ } catch (e: Exception) {
+ androidx.car.app.CarToast.makeText(context, "No navigation app available", androidx.car.app.CarToast.LENGTH_SHORT).show()
+ }
+ }
+
fun listLimit(context: CarContext): Int = runCatching {
context.getCarService(ConstraintManager::class.java)
.getContentLimit(ConstraintManager.CONTENT_LIMIT_TYPE_LIST)
diff --git a/app/src/main/java/com/rfsentinel/app/car/DeviceDetailScreen.kt b/app/src/main/java/com/rfsentinel/app/car/DeviceDetailScreen.kt
index 10795a8..022f6e0 100644
--- a/app/src/main/java/com/rfsentinel/app/car/DeviceDetailScreen.kt
+++ b/app/src/main/java/com/rfsentinel/app/car/DeviceDetailScreen.kt
@@ -122,7 +122,7 @@ class DeviceDetailScreen(carContext: CarContext, private val mac: String) : Live
Action.Builder()
.setTitle("Navigate")
.setIcon(CarUi.icon(carContext, R.drawable.ic_car_navigate))
- .setOnClickListener { navigateTo(lat, lon) }
+ .setOnClickListener { CarUi.navigateTo(carContext, lat, lon) }
.build()
)
}
@@ -158,15 +158,5 @@ class DeviceDetailScreen(carContext: CarContext, private val mac: String) : Live
}
/** Hands the point to the car's navigation app. */
- private fun navigateTo(lat: Double, lon: Double) {
- try {
- carContext.startCarApp(
- Intent(CarContext.ACTION_NAVIGATE, Uri.parse(String.format(Locale.US, "geo:%.7f,%.7f", lat, lon)))
- )
- } catch (e: Exception) {
- toast("No navigation app available")
- }
- }
-
private fun toast(msg: String) = CarToast.makeText(carContext, msg, CarToast.LENGTH_SHORT).show()
}
diff --git a/app/src/main/java/com/rfsentinel/app/car/DevicesMapScreen.kt b/app/src/main/java/com/rfsentinel/app/car/DevicesMapScreen.kt
index 45a9d65..3aa8d8e 100644
--- a/app/src/main/java/com/rfsentinel/app/car/DevicesMapScreen.kt
+++ b/app/src/main/java/com/rfsentinel/app/car/DevicesMapScreen.kt
@@ -89,10 +89,32 @@ class DevicesMapScreen(carContext: CarContext) : LiveScreen(carContext, periodMs
/**
* Devices with a position, most relevant first: flagged (strongest
- * evidence first), then ordinary by signal strength. Drones use their
- * own Remote ID position. Pure apart from the registry; unit-tested.
+ * evidence first), then ordinary ones alternating Bluetooth and WiFi
+ * (each by signal strength), with the access points of one WiFi network
+ * (2.4 / 5 GHz, mesh) merged - otherwise a home router fills every slot.
+ * Drones use their own Remote ID position. Pure apart from the registry;
+ * unit-tested.
*/
- fun around(lat: Double, lon: Double, limit: Int): List<Item> =
+ fun around(lat: Double, lon: Double, limit: Int): List<Item> {
+ val all = candidates(lat, lon)
+ val flagged = all.filter { it.item.flagged }.sortedByDescending { it.rank }
+ val ordinary = all.filterNot { it.item.flagged }.sortedByDescending { it.rank }
+ val wifi = ordinary.filter { it.wifi }
+ .distinctBy { c -> c.network?.lowercase() ?: c.item.mac } // strongest AP of each network
+ val ble = ordinary.filterNot { it.wifi }
+ val mixed = ArrayList<Candidate>()
+ var i = 0
+ while (i < maxOf(ble.size, wifi.size)) {
+ ble.getOrNull(i)?.let { mixed += it }
+ wifi.getOrNull(i)?.let { mixed += it }
+ i++
+ }
+ return (flagged + mixed).take(limit).map { it.item }
+ }
+
+ private class Candidate(val item: Item, val rank: Int, val wifi: Boolean, val network: String?)
+
+ private fun candidates(lat: Double, lon: Double): List<Candidate> =
DeviceRegistry.snapshot().mapNotNull { s ->
if (WhitelistCache.contains(s.mac)) return@mapNotNull null
val rid = s.remoteId?.takeIf { it.hasPosition }
@@ -100,7 +122,7 @@ class DevicesMapScreen(carContext: CarContext) : LiveScreen(carContext, periodMs
val pLon = rid?.longitude ?: s.bestPosition?.lon ?: return@mapNotNull null
val best = s.best
val flagged = DeviceColors.isFlagged(s)
- Item(
+ val item = Item(
mac = s.mac,
title = best?.label ?: s.name ?: s.deviceType,
// Short enough for one line on a car screen.
@@ -115,10 +137,14 @@ class DevicesMapScreen(carContext: CarContext) : LiveScreen(carContext, periodMs
else -> best.category.colorArgb
},
tag = best?.category?.shortTag?.take(1)
- ) to (if (flagged) 1_000 + (best?.confidence ?: 0) else 0) * 1_000 + (s.rssi + 200)
+ )
+ val wifi = s.source == com.rfsentinel.app.detect.Advert.Source.WIFI
+ Candidate(
+ item,
+ rank = (if (flagged) (best?.confidence ?: 0) else 0) * 1_000 + (s.rssi + 200),
+ wifi = wifi,
+ network = if (wifi) s.name?.takeIf { it.isNotBlank() } else null
+ )
}
- .sortedByDescending { it.second }
- .take(limit)
- .map { it.first }
}
}
diff --git a/app/src/main/java/com/rfsentinel/app/car/NearbyMapScreen.kt b/app/src/main/java/com/rfsentinel/app/car/NearbyMapScreen.kt
index 50f34bb..8465850 100644
--- a/app/src/main/java/com/rfsentinel/app/car/NearbyMapScreen.kt
+++ b/app/src/main/java/com/rfsentinel/app/car/NearbyMapScreen.kt
@@ -76,23 +76,21 @@ class NearbyMapScreen(
}
private fun row(it: Item): Row {
+ val color = when {
+ it.drone -> CarColor.BLUE
+ it.deviceColor != null -> CarColor.createCustom(it.deviceColor, it.deviceColor)
+ it.kind == com.rfsentinel.app.alpr.KnownCamera.Kind.ALPR -> CarColor.RED
+ else -> CarColor.YELLOW
+ }
+ val label = when (it.kind) {
+ null -> if (it.drone) "D" else "!"
+ com.rfsentinel.app.alpr.KnownCamera.Kind.SPEED -> "S"
+ com.rfsentinel.app.alpr.KnownCamera.Kind.RED_LIGHT -> "R"
+ com.rfsentinel.app.alpr.KnownCamera.Kind.ALPR -> "P"
+ }
val place = Place.Builder(CarLocation.create(it.lat, it.lon))
- .setMarker(
- PlaceMarker.Builder()
- .setColor(when {
- it.drone -> CarColor.BLUE
- it.deviceColor != null -> CarColor.createCustom(it.deviceColor, it.deviceColor)
- it.kind == com.rfsentinel.app.alpr.KnownCamera.Kind.ALPR -> CarColor.RED
- else -> CarColor.YELLOW
- })
- .setLabel(when (it.kind) {
- null -> if (it.drone) "D" else "!"
- com.rfsentinel.app.alpr.KnownCamera.Kind.SPEED -> "S"
- com.rfsentinel.app.alpr.KnownCamera.Kind.RED_LIGHT -> "R"
- com.rfsentinel.app.alpr.KnownCamera.Kind.ALPR -> "P"
- })
- .build()
- ).build()
+ .setMarker(PlaceMarker.Builder().setColor(color).setLabel(label).build())
+ .build()
// Android Auto requires every place row on a map template to carry its distance
// as a DistanceSpan (the host formats it in the driver's units); plain text throws.
val text = android.text.SpannableString(" · " + it.detail)
@@ -104,7 +102,7 @@ class NearbyMapScreen(
.setMetadata(Metadata.Builder().setPlace(place).build())
.setOnClickListener {
if (it.mac != null) screenManager.push(DeviceDetailScreen(carContext, it.mac))
- else CarToast.makeText(carContext, "${it.title}: ${distanceText(it.distanceM)} away", CarToast.LENGTH_LONG).show()
+ else screenManager.push(PlaceFocusScreen(carContext, it.title, it.detail, it.lat, it.lon, color, label))
}
.build()
}
diff --git a/app/src/main/java/com/rfsentinel/app/car/PlaceFocusScreen.kt b/app/src/main/java/com/rfsentinel/app/car/PlaceFocusScreen.kt
new file mode 100644
index 0000000..dd5c3b6
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/car/PlaceFocusScreen.kt
@@ -0,0 +1,74 @@
+package com.rfsentinel.app.car
+
+import android.text.SpannableString
+import android.text.Spanned
+import androidx.car.app.CarContext
+import androidx.car.app.model.Action
+import androidx.car.app.model.ActionStrip
+import androidx.car.app.model.CarColor
+import androidx.car.app.model.CarLocation
+import androidx.car.app.model.DistanceSpan
+import androidx.car.app.model.ItemList
+import androidx.car.app.model.Metadata
+import androidx.car.app.model.Place
+import androidx.car.app.model.PlaceListMapTemplate
+import androidx.car.app.model.PlaceMarker
+import androidx.car.app.model.Row
+import androidx.car.app.model.Template
+import com.rfsentinel.app.R
+import com.rfsentinel.app.service.DeviceRegistry
+
+/**
+ * One place on its own (e.g. a camera tapped in the list): the car map centres
+ * on it. Android Auto only lets navigation apps pan and zoom their map, so
+ * "Navigate" hands the spot to your navigation app, where you can move freely.
+ */
+class PlaceFocusScreen(
+ carContext: CarContext,
+ private val title: String,
+ private val detail: String,
+ private val lat: Double,
+ private val lon: Double,
+ private val color: CarColor,
+ private val label: String?
+) : LiveScreen(carContext, periodMs = 5_000L) {
+
+ override fun onGetTemplate(): Template {
+ val me = CarUi.currentLocation(carContext)
+ val marker = PlaceMarker.Builder().setColor(color)
+ label?.let { marker.setLabel(it) }
+ val place = Place.Builder(CarLocation.create(lat, lon)).setMarker(marker.build()).build()
+
+ val row = Row.Builder().setTitle(title).setMetadata(Metadata.Builder().setPlace(place).build())
+ if (me != null) {
+ // Map rows carry their distance as a DistanceSpan (the car formats it).
+ val text = SpannableString(" · $detail")
+ val d = DeviceRegistry.metersBetween(me.latitude, me.longitude, lat, lon)
+ text.setSpan(DistanceSpan.create(NearbyMapScreen.distanceOf(d)), 0, 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
+ row.addText(text)
+ } else {
+ // No position yet, so no distance: Android Auto only accepts a map row
+ // without a DistanceSpan when it's browsable - let it start navigation.
+ row.addText(detail)
+ .setBrowsable(true)
+ .setOnClickListener { CarUi.navigateTo(carContext, lat, lon) }
+ }
+
+ return PlaceListMapTemplate.Builder()
+ .setTitle(title)
+ .setHeaderAction(Action.BACK)
+ // Your own position off, so the map frames this place rather than both of you.
+ .setCurrentLocationEnabled(false)
+ .setActionStrip(
+ ActionStrip.Builder().addAction(
+ Action.Builder()
+ .setTitle("Navigate")
+ .setIcon(CarUi.icon(carContext, R.drawable.ic_car_navigate))
+ .setOnClickListener { CarUi.navigateTo(carContext, lat, lon) }
+ .build()
+ ).build()
+ )
+ .setItemList(ItemList.Builder().addItem(row.build()).build())
+ .build()
+ }
+}
diff --git a/app/src/test/java/com/rfsentinel/app/car/CarScreensTest.kt b/app/src/test/java/com/rfsentinel/app/car/CarScreensTest.kt
index fe11c6b..f176cc8 100644
--- a/app/src/test/java/com/rfsentinel/app/car/CarScreensTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/car/CarScreensTest.kt
@@ -149,6 +149,42 @@ class CarScreensTest {
ScanForegroundService.setLastFixForTest(null)
}
+ @Test
+ fun devicesMapMixesBluetoothAndWifiAndMergesOneNetwork() {
+ DeviceRegistry.clear(); DeviceRegistry.startSession()
+ val at = DeviceRegistry.GeoSample(System.currentTimeMillis(), 10.5, -20.5)
+ fun wifi(mac: String, ssid: String, rssi: Int) = Advert(mac, Advert.Source.WIFI, rssi, ssid,
+ wifi = Advert.WifiInfo(2437, "[WPA2-PSK-CCMP]", null, emptyList()))
+ // One home network seen as three access points, all stronger than the Bluetooth devices.
+ listOf(wifi("A0:00:00:00:00:01", "home", -40), wifi("A0:00:00:00:00:02", "home", -42),
+ wifi("A0:00:00:00:00:03", "home", -45), wifi("A0:00:00:00:00:04", "cafe", -70)).forEach {
+ DeviceRegistry.report(it, emptyList(), DeviceIntel.Identity("WiFi access point", emptyList()), null, null, at)
+ }
+ listOf(-60, -65, -75).forEachIndexed { i, rssi ->
+ DeviceRegistry.report(Advert("B0:00:00:00:00:0$i", Advert.Source.BLE, rssi, "Watch $i"), emptyList(),
+ DeviceIntel.Identity("Watch", emptyList()), null, null, at)
+ }
+ val macs = DevicesMapScreen.around(10.5, -20.5, 6).map { it.mac }
+ assertEquals(
+ listOf("B0:00:00:00:00:00", "A0:00:00:00:00:01", "B0:00:00:00:00:01", "A0:00:00:00:00:04", "B0:00:00:00:00:02"),
+ macs // Bluetooth / WiFi alternate; "home" appears once (its strongest AP)
+ )
+ }
+
+ @Test
+ fun cameraFocusScreenCentresOnOnePlaceWithNavigate() {
+ fun build() = PlaceFocusScreen(car, "Speed camera (50 km/h)", "Mapped in OpenStreetMap", 10.51, -20.5,
+ androidx.car.app.model.CarColor.YELLOW, "S").onGetTemplate() as androidx.car.app.model.PlaceListMapTemplate
+ // Without a position yet (no distance) and with one (DistanceSpan): both must build.
+ ScanForegroundService.setLastFixForTest(null)
+ assertEquals(1, build().itemList!!.items.size)
+ ScanForegroundService.setLastFixForTest(android.location.Location("test").apply { latitude = 10.5; longitude = -20.5 })
+ val t = build()
+ assertEquals(1, t.itemList!!.items.size)
+ assertEquals("Navigate", t.actionStrip!!.actions.single().title.toString())
+ ScanForegroundService.setLastFixForTest(null)
+ }
+
@Test
fun nearbyMapListsDronesThenClosestCameras() {
// Synthetic positions: you at 10.5, -20.5; the test drone is at the same point.