Skip to content

Commit

Permalink
nds2
Browse files Browse the repository at this point in the history
  • Loading branch information
bskjon committed Nov 11, 2024
1 parent 067bce3 commit f4c6371
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
32 changes: 32 additions & 0 deletions app/src/main/java/no/iktdev/demoapplication/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
package no.iktdev.demoapplication

import android.net.nsd.NsdServiceInfo
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import no.iktdev.demoapplication.databinding.ActivityMainBinding
import no.iktdev.networking.mdns.ndsDiscovery
import no.iktdev.networking.mdns.ndsDiscoveryListener
import java.net.InetAddress

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
setContentView(binding.root)

val ioScope = CoroutineScope(Job() + Dispatchers.IO)

ioScope.launch {
val discovery = ndsDiscovery(this@MainActivity, "_streamit._tcp", object: ndsDiscoveryListener {
override fun onResolveFailed(info: NsdServiceInfo, errorCode: Int) {
TODO("Not yet implemented")
}

override fun onResolved(info: NsdServiceInfo) {
TODO("Not yet implemented")
}

override fun onLost(hostAddress: InetAddress, serviceName: String) {
TODO("Not yet implemented")
}

override fun onStartFailed(serviceType: String, errorCode: Int) {
TODO("Not yet implemented")
}

})
discovery.startDiscovery()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,7 @@ class ndsDiscovery(context: Context, val serviceName: String, var listener: ndsD
}

override fun onServiceResolved(resolvedServiceInfo: NsdServiceInfo) {
val name = resolvedServiceInfo.serviceName
val host = resolvedServiceInfo.host?.hostAddress
val port = resolvedServiceInfo.port ?: 80
val attrs = resolvedServiceInfo.attributes.mapValues { String(it.value) }
val url = attrs["url"] ?: ""
Log.d("NsdHelper", "Service resolved: ${resolvedServiceInfo.host}")

host?.let {
ndsData(
name = name,
ip = it,
port = port,
url = url
)
}?.also { listener.onResolved(it) }
listener.onResolved(resolvedServiceInfo)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.net.InetAddress
interface ndsDiscoveryListener {

fun onResolveFailed(info: NsdServiceInfo, errorCode: Int)
fun onResolved(ndsData: ndsData)
fun onResolved(info: NsdServiceInfo)
fun onLost(hostAddress: InetAddress, serviceName: String)
fun onStartFailed(serviceType: String, errorCode: Int)
}

0 comments on commit f4c6371

Please sign in to comment.