コード例 #1
0
        @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();
            }
          }
        }
コード例 #2
0
 public synchronized void close() {
   ARSALPrint.v(TAG, "Closing MdsnSd based ARDiscovery");
   if (started) {
     stop();
   }
   mdnsSd.stop();
   this.broadcaster = null;
   this.context = null;
 }
コード例 #3
0
 public synchronized void stop() {
   if (started) {
     ARSALPrint.v(TAG, "Stopping MdsnSd based ARDiscovery");
     started = false;
     if (multicastLock.isHeld()) {
       multicastLock.release();
     }
     context.unregisterReceiver(networkStateIntentReceiver);
     mdnsSd.stop();
     netDeviceServicesHmap.clear();
     broadcaster.broadcastDeviceServiceArrayUpdated();
   }
 }
コード例 #4
0
        @Override
        public void onServiceAdded(
            String name, String serviceType, String ipAddress, int port, String[] txts) {
          ARSALPrint.d(
              TAG,
              "onServiceAdded : "
                  + name
                  + " |type : "
                  + serviceType
                  + " |ip : "
                  + ipAddress
                  + " |port : "
                  + port
                  + " |txts = "
                  + txts);
          String serialNumber = null;
          if (txts != null && txts.length >= 1) {
            serialNumber = txts[0];
          }
          ARDiscoveryDeviceNetService deviceNetService =
              new ARDiscoveryDeviceNetService(name, serviceType, ipAddress, port, serialNumber);

          ARDISCOVERY_PRODUCT_ENUM product = devicesServices.get(deviceNetService.getType());
          if (product != null) {
            int productID = ARDiscoveryService.nativeGetProductID(product.getValue());
            ARDiscoveryDeviceService deviceService =
                new ARDiscoveryDeviceService(name, deviceNetService, productID);
            netDeviceServicesHmap.put(deviceService.getName(), deviceService);
            if (broadcaster != null) {
              // got an answer, stop sending queries
              mdnsSd.cancelSendQueries();
              // broadcast the new deviceServiceList
              broadcaster.broadcastDeviceServiceArrayUpdated();
            }
          }
        }