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:
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();
}