Internal error using Network Services Discovery on Android

During the first implementation of NSDManager using examples and a tutorial on the developer's page, the application successfully launched discovery and discovered devices.

However, now it seems broken ...

When the program starts, after some initialization, the code enters the following method and starts successfully:

public void discoverServices() { Log.d(TAG, "Initializing discovery on NSD"); mNsdManager.discoverServices( SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener); } 

A log message has been received. After a while (say, about 5 minutes), this is output from the program:

 05-21 11:08:32.518: E/NsdCamera(12236): Discovery failed: Error code:0 05-21 11:08:32.518: W/dalvikvm(12236): threadid=12: thread exiting with uncaught exception (group=0x40c9c930) 05-21 11:08:32.518: E/AndroidRuntime(12236): FATAL EXCEPTION: NsdManager 05-21 11:08:32.518: E/AndroidRuntime(12236): java.lang.NullPointerException 05-21 11:08:32.518: E/AndroidRuntime(12236): at android.net.nsd.NsdManager$ServiceHandler.handleMessage(NsdManager.java:338) 05-21 11:08:32.518: E/AndroidRuntime(12236): at android.os.Handler.dispatchMessage(Handler.java:99) 05-21 11:08:32.518: E/AndroidRuntime(12236): at android.os.Looper.loop(Looper.java:137) 05-21 11:08:32.518: E/AndroidRuntime(12236): at android.os.HandlerThread.run(HandlerThread.java:60) 

Also from the services:

 05-21 11:50:49.108: E/NativeDaemonConnector.ResponseQueue(8858): Timeout waiting for response 05-21 11:50:49.108: E/mDnsConnector(8858): timed-out waiting for response to 10 mdnssd discover 6 _http._tcp. 05-21 11:50:49.108: E/NsdService(8858): Failed to discoverServices com.android.server.NativeDaemonConnector$NativeDaemonFailureException: command '10 mdnssd discover 6 _http._tcp.' failed with 'null' 

Error code "0" is described in

CameraChooseActivity -> onCreate calls a helper class

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camerachoose); //Setup the animation for the text in the Relativelayout mDescription = (TextSwitcher) findViewById(R.id.camera_add); mDescription.setFactory(this); mDescription.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); mDescription.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); mDescription.setText(getText(R.string.camera_add)); //Building alert dialog mBuilder = new AlertDialog.Builder(this,AlertDialog.THEME_HOLO_DARK); mBuilder.setMessage(R.string.dialog_about).setTitle(R.string.action_about); mBuilder.setIcon(android.R.drawable.ic_dialog_info); mLayout = (RelativeLayout) findViewById(R.id.layout_camerachoose); //Initialize the NSD mNSDHelper = new NsdCamera(this); mNSDHelper.initializeNsd(); 
+7
source share
2 answers

Based on my experience, I believe this is a lifelong listener problem.

Since you are supplying two listeners to the NSD service of the system, one for startServiceDiscovery () and one for stopServiceDiscovery (). you need to make sure that these listeners are still alive when the system addresses these listeners.

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

So, if the listener is a local object and is freed after calling startServiceDiscovery (), this may cause the NSD service to fail.

public void stopServiceDiscovery (NsdManager.DiscoveryListener Listener)

Disable service discovery initiated with findServices (). Active service discovery is reported to the application with onDiscoveryStarted (String), and it remains active until the application causes a service stop detection. A successful stop is notified with a call to onDiscoveryStopped (String).

After service discovery is denied, the application is notified via onStopDiscoveryFailed (String, int).

Listener Parameters This must be a listener object that was passed in to find the services (String, int, NsdManager.DiscoveryListener). This identifies the discovery that needs to be stopped and notifies a successful stop.

and below the snippet make sure you are not calling the 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.

+3
source

A simple restart of the DUT turned out to be the solution. I must say that the error is rather strange. I think the demon crashed and did not reboot.

(If someone can post an analysis or get a much better solution, submit it)

+3
source

All Articles