3

I am having a problem related to Network Service Discovery.

When I start the app with wifi connected, NSD Works Totally fine discovering the service and smoothly resolving them. But the problem arises when we connect wifi, after disabling wifi or switching the wifi from airplane mode.

It just gets stuck on DiscoveryStarted and never proceeds from there, although it establishes the connection to the wifi router after turning off airplane mode.

In code I have also ensured that the discovery will only start when the wifi connection is ensured but, no luck.

Right now I have to kill the app in order for NSD to work properly.

I am using NSD Helper from Google Gist:

https://android.googlesource.com/platform/development/+/master/samples/training/NsdChat/src/com/example/android/nsdchat/NsdHelper.java

NsdHelper helper;
    BroadcastReceiver wifiReciever = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
                if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) {
                    //do stuff
                    helper.stopDiscovery();
                    helper = new NsdHelper(context);
                    helper.discoverServices();
                } else {
                    // wifi connection was lost
                    helper.stopDiscovery();
                }
            }
        }
    };




    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //startDiscovery();
//        helper = new NsdHelper(this);
//        helper.discoverServices();

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
        registerReceiver(wifiReciever, intentFilter);




        Toast.makeText(this,"Service Started",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
//        if(service!= null)
//        {
//            service.stop();

//        helper.stopDiscovery();
//        }
        unregisterReceiver(wifiReciever);
//
       Toast.makeText(this,"Service destroyed",Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }
QA Collective
  • 2,222
  • 21
  • 34
  • If you add a log inside the `onReceive` and connect to wifi, you actually get the intent? and is the value you get actually true? please check – ronginat Apr 10 '19 at 11:19
  • @ronginat yes it recieves true it also starts the discovery but never finds the connected services from the router – Junaid Warsi Apr 10 '19 at 11:24
  • Why do you need a new helper? Can't you use the same instance? – ronginat Apr 10 '19 at 11:31
  • i did a lot of experiments in this code @ronginat so it was one of the experiments that i thought reinstantiating it will make a difference but no luck i was using the same instance earlier – Junaid Warsi Apr 10 '19 at 11:41

0 Answers0