Internal error using Network Service Discovery in Android

左心房为你撑大大i 提交于 2019-12-01 04:00:19

Base on my experience, I suppose this is a listener lifetime issue.

Because you supply two listeners to the system NSD service, one is for startServiceDiscovery() and another for stopServiceDiscovery(). you need to make sure these listeners still alive when the system accesses these listeners.

One fact is that onStartDiscoveryFailed() is called 2 minutes after startServiceDiscovery() is called, it should be a long time compared to the lifetime of the listener.

So if the listener is a local object and is released after calling the startServiceDiscovery(), it maybe cause the NSD service to crash.

public void stopServiceDiscovery (NsdManager.DiscoveryListener listener)

Stop service discovery initiated with discoverServices(). An active service discovery is notified to the application with onDiscoveryStarted(String) and it stays active until the application invokes a stop service discovery. A successful stop is notified to with a call to onDiscoveryStopped(String).

Upon failure to stop service discovery, application is notified through onStopDiscoveryFailed(String, int).

Parameters listener This should be the listener object that was passed to discoverServices(String, int, NsdManager.DiscoveryListener). It identifies the discovery that should be stopped and notifies of a successful stop.

and below snippet make sure do not call any NsdManager api.

@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
     Log.i(TAG, "onStartDiscoveryFailed : Error code:" + errorCode);
}

@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
    Log.i(TAG, "onStopDiscoveryFailed : Error code:" + errorCode);
}

Good Luck.

A simple restart of the DUT proved to be the solution. Must say that the error is pretty odd. I think the daemon crashed and didn't restart.

(If someone can post an analysis or have a much better solution, please post it)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!