diff --git a/echolot-app/adb-beacon/src/main/kotlin/app/echo_lot/adbbeacon/BeaconService.kt b/echolot-app/adb-beacon/src/main/kotlin/app/echo_lot/adbbeacon/BeaconService.kt index 48395f6..dfd2eb7 100644 --- a/echolot-app/adb-beacon/src/main/kotlin/app/echo_lot/adbbeacon/BeaconService.kt +++ b/echolot-app/adb-beacon/src/main/kotlin/app/echo_lot/adbbeacon/BeaconService.kt @@ -62,7 +62,13 @@ class BeaconService : Service() { override fun onStopDiscoveryFailed(t: String?, code: Int) {} override fun onDiscoveryStarted(t: String?) {} override fun onDiscoveryStopped(t: String?) {} - override fun onServiceLost(s: NsdServiceInfo?) { currentPort = -1 } + override fun onServiceLost(s: NsdServiceInfo?) { + // adbd stops advertising when Wireless debugging is turned off. + currentPort = -1 + val warn = "⚠ Wireless debugging appears OFF (adb mDNS service gone) — re-enable it" + Status.set(warn) + updateNotification(warn) + } override fun onServiceFound(s: NsdServiceInfo?) { if (s == null) return resolve(s) @@ -79,8 +85,14 @@ class BeaconService : Service() { nsd.resolveService(info, object : NsdManager.ResolveListener { override fun onResolveFailed(s: NsdServiceInfo?, code: Int) {} override fun onServiceResolved(s: NsdServiceInfo?) { - val port = s?.port ?: return - currentPort = port + s ?: return + // On a shared LAN, NsdManager discovers EVERY device's adb + // advertisement — accept only the one whose host is THIS device's + // own IP, else we'd report a neighbour's port for our IP. + val host = s.host?.hostAddress + val mine = wifiIpv4() + if (host != null && mine != null && host != mine) return + currentPort = s.port scope.launch { report() } } }) @@ -89,7 +101,13 @@ class BeaconService : Service() { private fun report() { val ip = wifiIpv4() ?: run { Status.set("no wlan0 IPv4 (is wifi up?)"); return } val port = currentPort - if (port <= 0) { Status.set("$ip — waiting for wireless-debug port"); return } + if (port <= 0) { + // No adb advertisement: either discovery hasn't landed yet, or (usually) Wireless + // debugging is off. Say so plainly. + val warn = "⚠ $ip — no wireless-debug port. Is Wireless debugging ON?" + Status.set(warn); updateNotification(warn) + return + } val device = android.os.Build.MODEL.replace(Regex("[^A-Za-z0-9_.-]"), "_") val body = """{"device":"$device","ip":"$ip","port":$port}""" // Send over a VALIDATED internet network — the wireless-debug wifi is often a restricted