IP address of device using phone as access point

前端 未结 3 929
长发绾君心
长发绾君心 2020-12-05 22:23

Can anyone explain or show how to get the IP address of a computer (or other device) that\'s connected through the phone\'s portable WI-FI hotspot?

I tried the foll

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 22:42

    Though the answer if given, but in case anyone is interested in direct answer:

    private static ArrayList readArpCache()
       {
          ArrayList ipList = new ArrayList();
          BufferedReader br = null;
          try {
             br = new BufferedReader(new FileReader("/proc/net/arp"), 1024);
             String line;
             while ((line = br.readLine()) != null) {
                Log.d(TAG  ,line);
    
                String[] tokens = line.split(" +");
                if (tokens != null && tokens.length >= 4) {
                   // verify format of MAC address
                   String macAddress = tokens[3];
                   if (macAddress.matches("..:..:..:..:..:..")) {
                      //Log.i(TAG, "MAC=" + macAddress + " IP=" + tokens[0] + " HW=" + tokens[1]);
    
                      // Ignore the entries with MAC-address "00:00:00:00:00:00"
                      if (!macAddress.equals("00:00:00:00:00:00")) {
    
                         String ipAddress = tokens[0];
                         ipList.add(ipAddress);
    
                         Log.i(TAG, macAddress + "; " + ipAddress);
                      }
                   }
                }
             }
          } catch (Exception e) {
             e.printStackTrace();
          } finally {
             try {
                br.close();
             } catch (IOException e) {
                e.printStackTrace();
             }
          }
    
          return ipList;
       }
    

    Also as per my understanding (didn't try by myself), if you are using aysntask and need to do on worker thread(won't work on main thread), this process can also be used. Either use method to find hotspotinterface or directly use _hotspotInterface as "wlan0"

    try {
             InetAddress[] arr = InetAddress.getAllByName(_hotspotInterface);
    
          } catch (UnknownHostException e) {
             e.printStackTrace();
          } catch (Exception e) {
             e.printStackTrace();
          }
    

    To find hotspot I used this method(though not very good example)

    String _hotspotInterface = "";
         for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); )
                 {
                    NetworkInterface anInterface = en.nextElement();
    
                    //look for 192.168.xx.xx first
                    for (Enumeration enumIpAddr = anInterface.getInetAddresses(); enumIpAddr.hasMoreElements(); )
                    {
                       InetAddress inetAddress = enumIpAddr.nextElement();
                       String       hostAddress = inetAddress.getHostAddress();
    
                       if (hostAddress.startsWith("192.168."))
                       {
                          _hotspotInterface = anInterface.getName();
                       }
                    }
    }
    

提交回复
热议问题