Android NsdManager 无法发现服务

Android NsdManager not able to discover services

我 运行 遇到 Android NsdManager when following their tutorial Using Network Service Discovery 的问题。

我的网络上有几个 zeroconf/bonjour 硬件设备。从我的 mac 我可以通过以下命令从我的终端中发现所有这些 dns-sd -Z _my-mesh._tcp.

从我的 Android 应用程序的第一个 运行 我可以使用 NsdManager 完美地发现这些服务。但是,如果我重新启动应用程序并重试,则会找到 none 的服务。 onDiscoveryStarted 被成功调用,但之后没有其他任何事情。在等待期间,我可以从 mac 确认服务仍然成功。

然后我可以打开我的 Zeroconf 应用程序(在 Android 上),它会显示像我的 mac 这样的服务。当我 return 我的应用程序时,我看到它立即收到我之前预期的所有回调。所以我相信我的方法有问题,但我不确定是什么。下面是我用来发现和解析服务的代码。该视图是一个巨大的文本视图(在滚动视图中)我一直在写文本以便于调试。

import android.annotation.SuppressLint
import android.content.Context
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class MainActivity : AppCompatActivity(),
                     NsdManager.DiscoveryListener {

    private var nsdManager: NsdManager? = null
    private var text: TextView? = null
    private var isResolving = false
    private val services = ArrayList<ServiceWrapper>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        this.text = findViewById(R.id.text)
        this.nsdManager = application.getSystemService(Context.NSD_SERVICE) as NsdManager

    }

    override fun onResume() {
        super.onResume()
        this.nsdManager?.discoverServices("_my-mesh._tcp.", NsdManager.PROTOCOL_DNS_SD, this)
        write("Resume Discovering Services")
    }

    override fun onPause() {
        super.onPause()
        this.nsdManager?.stopServiceDiscovery(this)
        write("Pause Discovering Services")
    }

    override fun onServiceFound(serviceInfo: NsdServiceInfo?) {
        write("onServiceFound(serviceInfo = $serviceInfo))")
        if (serviceInfo == null) {
            return
        }
        add(serviceInfo)
    }

    override fun onStopDiscoveryFailed(serviceType: String?, errorCode: Int) {
        write("onStopDiscoveryFailed(serviceType = $serviceType, errorCode = $errorCode)")
    }

    override fun onStartDiscoveryFailed(serviceType: String?, errorCode: Int) {
        write("onStartDiscoveryFailed(serviceType = $serviceType, errorCode = $errorCode)")
    }

    override fun onDiscoveryStarted(serviceType: String?) {
        write("onDiscoveryStarted(serviceType = $serviceType)")
    }

    override fun onDiscoveryStopped(serviceType: String?) {
        write("onDiscoveryStopped(serviceType = $serviceType)")
    }

    override fun onServiceLost(serviceInfo: NsdServiceInfo?) {
        write("onServiceLost(serviceInfo = $serviceInfo)")
    }

    private fun createResolveListener(): NsdManager.ResolveListener {
        return object : NsdManager.ResolveListener {
            override fun onResolveFailed(serviceInfo: NsdServiceInfo?, errorCode: Int) {
                write("onResolveFailed(serviceInfo = $serviceInfo, errorCode = $errorCode)")
                isResolving = false
                resolveNext()
            }

            override fun onServiceResolved(serviceInfo: NsdServiceInfo?) {
                write("onServiceResolved(serviceInfo = $serviceInfo)")
                if (serviceInfo == null) {
                    return
                }
                for (servicewrapper in services) {
                    if (servicewrapper.serviceInfo.serviceName == serviceInfo.serviceName) {
                        servicewrapper.resolve(serviceInfo)
                    }
                }
                isResolving = false
                resolveNext()
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun write(text: String?) {
        this.text?.let {
            it.post({
                        it.text = it.text.toString() + "\n" + text + "\n"
                    })
        }
    }

    fun add(serviceInfo: NsdServiceInfo) {
        for (servicewrapper in services) {
            if (servicewrapper.serviceInfo.serviceName == serviceInfo.serviceName) {
                return
            }
        }
        services.add(ServiceWrapper(serviceInfo))
        resolveNext()
    }

    @Synchronized
    fun resolveNext() {
        if (isResolving) {
            return
        }
        isResolving = true
        for (servicewrapper in services) {
            if (servicewrapper.isResolved) {
                continue
            }
            write("resolving")
            this.nsdManager?.resolveService(servicewrapper.serviceInfo, createResolveListener())
            return
        }
        isResolving = false

    }

    inner class ServiceWrapper(var serviceInfo: NsdServiceInfo) {

        var isResolved = false

        fun resolve(serviceInfo: NsdServiceInfo) {
            isResolved = true
            this.serviceInfo = serviceInfo


        }

    }


}

迟到总比不到好。直到现在才意识到其他人也有这个问题。

我们发现一些路由器正在阻止或无法正确地来回转发数据包。我们对此的解决方案是使用 wire shark 来检测其他流行的应用程序正在做什么来解决这个问题。 Android 的 NsdManager 的可定制性有限,因此需要通过 MulticastSocket 手动传输数据包。

interface NsdDiscovery {

suspend fun startDiscovery()
suspend fun stopDiscovery()
fun setListener(listener: Listener?)
fun isDiscovering(): Boolean

interface Listener {

    fun onServiceFound(ip:String, local:String)
    fun onServiceLost(event: ServiceEvent)

}

}

@Singleton
class ManualNsdDiscovery @Inject constructor()
: NsdDiscovery {

//region Fields

private val isDiscovering = AtomicBoolean(false)

private var socketManager: SocketManager? = null

private var listener: WeakReference<NsdDiscovery.Listener> = WeakReference<NsdDiscovery.Listener>(null)

//endregion

//region NsdDiscovery

override suspend fun startDiscovery() = withContext(Dispatchers.IO) {
    if (isDiscovering()) return@withContext
    this@ManualNsdDiscovery.isDiscovering.set(true)
    val socketManager = SocketManager()
    socketManager.start()
    this@ManualNsdDiscovery.socketManager = socketManager

}

override suspend fun stopDiscovery() = withContext(Dispatchers.IO) {
    if (!isDiscovering()) return@withContext
    this@ManualNsdDiscovery.socketManager?.stop()
    this@ManualNsdDiscovery.socketManager = null
    this@ManualNsdDiscovery.isDiscovering.set(false)
}

override fun setListener(listener: NsdDiscovery.Listener?) {
    this.listener = WeakReference<NsdDiscovery.Listener>(listener)
}

@Synchronized
override fun isDiscovering(): Boolean {
    return this.isDiscovering.get()
}

//endregion

private inner class SocketManager {

    //region Fields

    private val group = InetAddress.getByName("224.0.0.251")
                        ?: throw IllegalStateException("Can't setup group")

    private val incomingNsd = IncomingNsd()

    private val outgoingNsd = OutgoingNsd()

    //endregion

    //region Constructors


    //endregion

    //region Methods

    suspend fun start() {
        this.incomingNsd.startListening()
        this.outgoingNsd.send()
    }

    fun stop() {
        this.incomingNsd.stopListening()
    }


    //endregion


    private inner class OutgoingNsd {

        //region Fields

        private val socketMutex = Mutex()

        private var socket = MulticastSocket(5353)

        suspend fun setUpSocket() {
            this.socketMutex.withLock {
                try {
                    this.socket = MulticastSocket(5353)
                    this.socket.reuseAddress = true
                    this.socket.joinGroup(group)
                } catch (e: SocketException) {
                    return
                }
            }
        }

        suspend fun tearDownSocket() {
            this.socketMutex.withLock {
                this@OutgoingNsd.socket.close()
            }
        }

        //ugly code but here is the packet
        private val bytes = byteArrayOf(171.toByte(), 205.toByte(), 1.toByte(), 32.toByte(),
                                        0.toByte(), 1.toByte(), 0.toByte(), 0.toByte(),
                                        0.toByte(), 0.toByte(), 0.toByte(), 0.toByte(),
                                        9.toByte(), 95.toByte(), 101.toByte(), 118.toByte(),
                                        97.toByte(), 45.toByte(), 109.toByte(), 101.toByte(),
                                        115.toByte(), 104.toByte(), 4.toByte(), 95.toByte(),
                                        116.toByte(), 99.toByte(), 112.toByte(), 5.toByte(),
                                        108.toByte(), 111.toByte(), 99.toByte(), 97.toByte(),
                                        108.toByte(), 0.toByte(), 0.toByte(), 12.toByte(),
                                        0.toByte(), 1.toByte())


        private val outPacket = DatagramPacket(bytes,
                                               bytes.size,
                                               this@SocketManager.group,
                                               5353)

        //endregion
        //region Methods

        @Synchronized
        suspend fun send() {
            withContext(Dispatchers.Default) {
                setUpSocket()
                try {
                    this@OutgoingNsd.socket.send(this@OutgoingNsd.outPacket)
                    delay(1500L)
                    tearDownSocket()
                } catch (e: Exception) {

                }

            }
        }

        //endregion

    }

    private inner class IncomingNsd {

        //region Fields


        private val isRunning = AtomicBoolean(false)

        private var socket = MulticastSocket(5353)

        //endregion

        //region Any

        fun setUpSocket() {
            try {
                this.socket = MulticastSocket(5353)
                this.socket.reuseAddress = true
                this.socket.joinGroup(group)
            } catch (e: SocketException) {

            } catch (e: BindException) {

            }
        }

        fun run() {
            GlobalScope.launch(Dispatchers.Default) {
                setUpSocket()
                try {
                    while (this@IncomingNsd.isRunning.get()) {
                        val bytes = ByteArray(4096)
                        val inPacket = DatagramPacket(bytes, bytes.size)
                        this@IncomingNsd.socket.receive(inPacket)
                        val incoming = DNSIncoming(inPacket)
                        for (answer in incoming.allAnswers) {
                            if (answer.key.contains("_my_mesh._tcp")) {
                                this@ManualNsdDiscovery.listener.get()?.onServiceFound(answer.recordSource.hostAddress, answer.name)
                                return@launch
                            }
                        }

                    }
                    this@IncomingNsd.socket.close()
                } catch (e: Exception) {

                }
            }
        }

        //endregion

        //region Methods

        @Synchronized
        fun startListening() {
            if (this.isRunning.get()) {
                return
            }
            this.isRunning.set(true)
            run()
        }

        @Synchronized
        fun stopListening() {
            if (!this.isRunning.get()) {
                return
            }
            this.isRunning.set(false)
        }

        //endregion

    }

}

}