3

목표 - 내 응용 프로그램에서 인쇄하려면 Brother QL-720NW 레이블 프린터를 wifi에서 검색해야합니다.네트워크 장치의 IP 주소 목록에서 프린터 IP 주소를 찾습니다.

나는, SO와 같은 Get the IP address of printer에 다양한 유사한 질문을 통해 How to get IP adress of other hosts in same wifi network in android?

How to connect a network printer over Android?을 갔다 그러나 위의 어느 것도 완전히 내 문제를 해결할 수 없습니다. 사용

내 와이파이 네트워크에있는 모든 IP 주소의 목록을 얻을 수있어이 How to get IP address of the device from code? .

CODE :

String myIpAdd= getIPAddress(true); 
ArrayList<InetAddress> inetAddresses=getConnectedDevices(myIpAdd); 

public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) { 
     ArrayList<InetAddress> ret = new ArrayList<InetAddress>(); 

     LoopCurrentIP = 0; 

     String IPAddress = ""; 
     String[] myIPArray = YourPhoneIPAddress.split("\\."); 
     InetAddress currentPingAddr; 

     for (int i = 0; i <= 255; i++) { 
      try { 

       // build the next IP address 
       currentPingAddr = InetAddress.getByName(myIPArray[0] + "." + 
         myIPArray[1] + "." + 
         myIPArray[2] + "." + 
         Integer.toString(LoopCurrentIP)); 

       // 50ms Timeout for the "ping" 
       if (currentPingAddr.isReachable(50)) { 

        ret.add(currentPingAddr); 
       } 
      } catch (UnknownHostException ex) { 
       ex.printStackTrace(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

      LoopCurrentIP++; 
     } 
     return ret; 
    } 

    /** 
    * Get IP address from first non-localhost interface 
    * @param ipv4 true=return ipv4, false=return ipv6 
    * @return address or empty string 
    */ 
    public static String getIPAddress(boolean useIPv4) { 
     try { 
      List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); 
      for (NetworkInterface intf : interfaces) { 
       List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); 
       for (InetAddress addr : addrs) { 
        if (!addr.isLoopbackAddress()) { 
         String sAddr = addr.getHostAddress().toUpperCase(); 
         boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); 
         if (useIPv4) { 
          if (isIPv4) 
           return sAddr; 
         } else { 
          if (!isIPv4) { 
           int delim = sAddr.indexOf('%'); // drop ip6 port suffix 
           return delim<0 ? sAddr : sAddr.substring(0, delim); 
          } 
         } 
        } 
       } 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } // for now eat exceptions 
     return ""; 
    } 

어떻게 IP 주소 목록에서 내 프린터의 어떤 IP를 주소를 검색 할 수 있습니다?

도와주세요.

답변

0

정적 IP 주소를 유지하려는 경우 프린터에 설정할 수 있습니다. DHCP를 사용하고 있다면 새로운 IP가 할당 될 것입니다. 정적 인 경우 앱에서 코드를 작성하거나 설정에 넣을 수 있습니다. 그렇게하면 IP 주소를 통해 검색 할 필요가 없습니다.

0

글쎄, Rachita, 소켓을 통해 연결하고 프린터를 찾는 포트 9100에서 테스트하는 코드를 추가합니다 (목록을 추가하기 직전). Here is an example. 희망은 도움이

관련 문제