Skip to content

Commit

Permalink
feat: add meshtastic service type to mDNS service discovery (#1401)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir authored Nov 12, 2024
1 parent d324f77 commit a6e7a0e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a6e7a0e

Please sign in to comment.