AeroToss: TCP discovery вместо mDNS + IP в UI
- Заменён mDNS на TCP discovery (надёжнее, работает через NAT) - Desktop: TCP сервер на порту 9998 + UDP broadcast на порту 9999 - Phone: TCP клиент подключается к desktop для получения инфы - Protocol: DISCOVER → JSON response (deviceId, name, type, port, host) - DeviceCard: отображает IP адрес и порт под именем - Убрана зависимость от mDNS (может быть добавлена позже как fallback)
This commit is contained in:
@@ -18,7 +18,7 @@ class CompositeAndroidDiscovery(
|
||||
private val _devices = MutableStateFlow<List<Device>>(emptyList())
|
||||
override val devices: Flow<List<Device>> = _devices.asStateFlow()
|
||||
|
||||
private val udpDiscovery = UdpBroadcastDiscovery()
|
||||
private val tcpDiscovery = TcpDiscovery()
|
||||
|
||||
@Volatile private var scope: CoroutineScope? = null
|
||||
@Volatile private var discoveryJob: kotlinx.coroutines.Job? = null
|
||||
@@ -26,21 +26,18 @@ class CompositeAndroidDiscovery(
|
||||
override fun startDiscovery(servicePort: Int) {
|
||||
if (discoveryJob != null) return
|
||||
|
||||
nsdDiscovery.startDiscovery(servicePort)
|
||||
udpDiscovery.startDiscovery(servicePort)
|
||||
tcpDiscovery.startDiscovery(servicePort)
|
||||
wifiDirectManager.initialize()
|
||||
wifiDirectManager.discoverPeers()
|
||||
|
||||
scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
|
||||
discoveryJob = scope?.launch {
|
||||
combine(
|
||||
nsdDiscovery.devices,
|
||||
udpDiscovery.devices,
|
||||
tcpDiscovery.devices,
|
||||
wifiDirectManager.devices
|
||||
) { nsdDevices, udpDevices, wifiDirectDevices ->
|
||||
) { tcpDevices, wifiDirectDevices ->
|
||||
val allDevices = mutableMapOf<String, Device>()
|
||||
nsdDevices.forEach { allDevices["nsd:${it.id}"] = it }
|
||||
udpDevices.forEach { allDevices["udp:${it.id}"] = it }
|
||||
tcpDevices.forEach { allDevices["tcp:${it.id}"] = it }
|
||||
wifiDirectDevices.forEach { allDevices["wifi:${it.id}"] = it }
|
||||
allDevices.values.toList()
|
||||
}.collect { devices ->
|
||||
@@ -52,8 +49,7 @@ class CompositeAndroidDiscovery(
|
||||
override fun stopDiscovery() {
|
||||
discoveryJob?.cancel()
|
||||
discoveryJob = null
|
||||
nsdDiscovery.stopDiscovery()
|
||||
udpDiscovery.stopDiscovery()
|
||||
tcpDiscovery.stopDiscovery()
|
||||
wifiDirectManager.stopDiscovery()
|
||||
_devices.value = emptyList()
|
||||
}
|
||||
@@ -62,8 +58,7 @@ class CompositeAndroidDiscovery(
|
||||
stopDiscovery()
|
||||
scope?.cancel()
|
||||
scope = null
|
||||
nsdDiscovery.dispose()
|
||||
udpDiscovery.dispose()
|
||||
tcpDiscovery.dispose()
|
||||
wifiDirectManager.cleanup()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user