feat: add meshtastic service type to mDNS service discovery (#1401)

This commit is contained in:
Andre K 2024-11-12 17:14:58 -03:00 committed by GitHub
parent d324f77d63
commit a6e7a0ef4f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 2 deletions

View file

@ -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.")
}
}

View file

@ -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<String>,
serviceName: String,
): Flow<List<NsdServiceInfo>> {
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,