Skip to content

Commit 09738d6

Browse files
committed
#1977 potentially reduce the amount of "Failed to discover ADB port" errors
1 parent 59f3369 commit 09738d6

File tree

1 file changed

+3
-16
lines changed
  • sysbridge/src/main/java/io/github/sds100/keymapper/sysbridge/adb

1 file changed

+3
-16
lines changed

sysbridge/src/main/java/io/github/sds100/keymapper/sysbridge/adb/AdbMdns.kt

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import kotlinx.coroutines.channels.trySendBlocking
1717
import kotlinx.coroutines.flow.MutableStateFlow
1818
import kotlinx.coroutines.flow.first
1919
import kotlinx.coroutines.flow.update
20-
import kotlinx.coroutines.sync.Mutex
21-
import kotlinx.coroutines.sync.withLock
2220
import kotlinx.coroutines.withContext
2321
import kotlinx.coroutines.withTimeout
2422
import timber.log.Timber
@@ -40,8 +38,6 @@ internal class AdbMdns(ctx: Context, private val serviceType: AdbServiceType) {
4038
private var serviceResolvedChannel: Channel<NsdServiceInfo?>? = null
4139

4240
private val isDiscovering: MutableStateFlow<Boolean> = MutableStateFlow(false)
43-
private val discoveredPort: MutableStateFlow<Int?> = MutableStateFlow(null)
44-
private val discoverMutex: Mutex = Mutex()
4541

4642
private val resolveListener: NsdManager.ResolveListener = object : NsdManager.ResolveListener {
4743
override fun onResolveFailed(nsdServiceInfo: NsdServiceInfo, i: Int) {
@@ -106,19 +102,10 @@ internal class AdbMdns(ctx: Context, private val serviceType: AdbServiceType) {
106102

107103
@OptIn(ExperimentalCoroutinesApi::class)
108104
suspend fun discoverPort(): Int? {
109-
discoverMutex.withLock {
110-
val currentPort = discoveredPort.value
111-
112-
if (currentPort == null || !isPortAvailable(currentPort)) {
113-
val port = withContext(Dispatchers.IO) {
114-
discoverPortInternal()
115-
}
116-
discoveredPort.value = port
117-
return port
118-
} else {
119-
return currentPort
120-
}
105+
val port = withContext(Dispatchers.IO) {
106+
discoverPortInternal()
121107
}
108+
return port
122109
}
123110

124111
@OptIn(ExperimentalCoroutinesApi::class)

0 commit comments

Comments
 (0)