@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { mdnsSd.stop(); ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mEth = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET); NetworkInterface netInterface = null; // search the network interface with the ip address returned by the wifi manager if ((mWifi != null) && (mWifi.isConnected())) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (wifiManager != null) { WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddressInt = wifiInfo.getIpAddress(); String ipAddress = String.format( "%d.%d.%d.%d", (ipAddressInt & 0xff), (ipAddressInt >> 8 & 0xff), (ipAddressInt >> 16 & 0xff), (ipAddressInt >> 24 & 0xff)); try { InetAddress addr = InetAddress.getByName(ipAddress); Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces(); while (netInterface == null && intfs.hasMoreElements()) { NetworkInterface intf = intfs.nextElement(); Enumeration<InetAddress> interfaceAddresses = intf.getInetAddresses(); while (netInterface == null && interfaceAddresses.hasMoreElements()) { InetAddress interfaceAddr = interfaceAddresses.nextElement(); if (interfaceAddr.equals(addr)) { netInterface = intf; } } } } catch (Exception e) { ARSALPrint.e(TAG, "Unable to get the wifi network interface", e); } } } // for ethernet, it's not possible to find the correct netInterface. Assume there is // a default route don't specify the netinterface if (((mWifi != null) && (mWifi.isConnected())) || ((mEth != null) && (mEth.isConnected()))) { ARSALPrint.v(TAG, "Restaring MdsnSd"); mdnsSd.start(netInterface); } else { netDeviceServicesHmap.clear(); broadcaster.broadcastDeviceServiceArrayUpdated(); } } }