From a6e7a0ef4fc24510021a842c8bd0f4dbd6ac51b7 Mon Sep 17 00:00:00 2001 From: Andre K Date: Tue, 12 Nov 2024 17:14:58 -0300 Subject: [PATCH] feat: add `meshtastic` service type to mDNS service discovery (#1401) --- .../mesh/repository/network/NetworkRepository.kt | 4 ++-- .../com/geeksville/mesh/repository/network/NsdManager.kt | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/repository/network/NetworkRepository.kt b/app/src/main/java/com/geeksville/mesh/repository/network/NetworkRepository.kt index e4fc30496..47af07f94 100644 --- a/app/src/main/java/com/geeksville/mesh/repository/network/NetworkRepository.kt +++ b/app/src/main/java/com/geeksville/mesh/repository/network/NetworkRepository.kt @@ -16,11 +16,11 @@ class NetworkRepository @Inject constructor( val networkAvailable get() = connectivityManager.get().networkAvailable() val resolvedList - get() = nsdManagerLazy.get()?.serviceList(SERVICE_TYPE, SERVICE_NAME) ?: flowOf(emptyList()) + get() = nsdManagerLazy.get()?.serviceList(SERVICE_TYPES, SERVICE_NAME) ?: flowOf(emptyList()) companion object { // To find all available services use SERVICE_TYPE = "_services._dns-sd._udp" internal const val SERVICE_NAME = "Meshtastic" - internal const val SERVICE_TYPE = "_https._tcp." + internal val SERVICE_TYPES = listOf("_http._tcp.", "_meshtastic._tcp.") } } diff --git a/app/src/main/java/com/geeksville/mesh/repository/network/NsdManager.kt b/app/src/main/java/com/geeksville/mesh/repository/network/NsdManager.kt index 589591d4c..76bbdab89 100644 --- a/app/src/main/java/com/geeksville/mesh/repository/network/NsdManager.kt +++ b/app/src/main/java/com/geeksville/mesh/repository/network/NsdManager.kt @@ -7,11 +7,20 @@ import kotlinx.coroutines.cancel import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.suspendCancellableCoroutine import java.util.concurrent.CopyOnWriteArrayList import kotlin.coroutines.resume +internal fun NsdManager.serviceList( + serviceTypes: List, + serviceName: String, +): Flow> { + val flows = serviceTypes.map { serviceType -> serviceList(serviceType, serviceName) } + return combine(flows) { lists -> lists.flatMap { it }.distinctBy { it.serviceName } } +} + @OptIn(ExperimentalCoroutinesApi::class) internal fun NsdManager.serviceList( serviceType: String,