【问题标题】:Android Find the device's ip address when it's hosting a hotspotAndroid 在托管热点时查找设备的 IP 地址
【发布时间】:2014-03-15 07:42:32
【问题描述】:

我需要在设备托管热点时找到设备的 IP 地址。到目前为止,我已经使用过这段代码:

//if is using Hotspot
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
    NetworkInterface intf = en.nextElement();
    if (intf.getName().contains("wlan")) {
        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
            InetAddress inetAddress = enumIpAddr.nextElement();
            if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) {
                return inetAddress.getHostAddress();
            }
        }
    }
}

这很好用,但某些设备上的 wifi NetworkInterface 名称不同。所以我必须先找到设备的 wifi NetworkInterface 名称(对于它的热点)。我怎样才能找到这个名字?还是有更好的方法来查找设备的 IP 地址?

/// 通过MAC查找正确的IP地址似乎也不起作用

【问题讨论】:

    标签: android wifi ip-address android-wifi android-networking


    【解决方案1】:

    user2224350 说:“我最近发现 WifiAP ip 地址在 Android 中是硬编码的。”

    除非用户手动更改了这个值(我认为这很不常见),否则使用硬编码值是绝对足够的。我认为这是最好的方法。 IP 地址为“192.168.43.1”,as seen here

    这适用于我的三星 A3 和华为 PRA-LX1,但 HTC Desires 530 也返回 192.168.1.1

    【讨论】:

      【解决方案2】:

      当您想在同一设备上获取热点设备的 ip 但不知道如何在连接的设备上获取它时,此解决方案有效 (服务器的 ip 大多是 192.168.43.1 你可以通过硬编码来完成工作)

       public void serverip()
      
          {
           DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
                  if (dhcpInfo==null){
                      Toast.makeText(MainActivity.this, "No ip for server", Toast.LENGTH_SHORT).show();
                  }else{
      
                      Toast.makeText(MainActivity.this, "ip of server "+android.text.format.Formatter.formatIpAddress(dhcpInfo.gateway), Toast.LENGTH_SHORT).show();
          }
      
      
                  }
      

      【讨论】:

        【解决方案3】:

        这可以帮助你。

        调用 shell 以请求网络适配器并检查您需要的网络适配器,在这种情况下是 wlan,因此请遵循此代码

        Process p=Runtime.getRuntime().exec("ip link show | grep -o \": wlan[0-9]:\" ");
        
        BufferedReader readCommandOutput = 
            new BufferedReader(new InputStreamReader(p.getInputStream()));
        
        String line=null;
        while((line=readCommandOutput.readLine())!=null){
            // Lines containing wlan will be returned
            // parse to get the name of that interface.
            String interface=line.split(":")[1];
            //this is the interface you need.
        }
        
        if (line==null){
            //nothing was found.
        }
        
        readCommandOutput.close();
        

        附注

        或者,您可以使用 Play 商店中的 Android 终端模拟器,以便在 android shell 中运行此命令。

        用于检索 IP 地址

        WifiManager wifi=(WifiManager)getSystemService(WIFI_SERVICE);
        int address=wifi.getDhcpInfo().ipAddress;
        Toast.makeText(this,intToIP(address),1).show();         
        
        
        public String intToIP(int i) {
            return ((i & 0xFF) 
                    + "." + ((i >> 8) & 0xFF)
                    + "." + ((i >> 16) & 0xFF)
                    + "." + ((i >> 24) & 0xFF));
         }
        

        【讨论】:

        • 这没有任何好处。它只缓存名为wlanX 的设备。有设备命名有wifi接口ethX
        • 如果您需要其他人,您只需删除正则表达式中的 wlan 但如果您正确阅读它的问题,他需要 wifi 接口的名称。所以我在里面提到了 wlan。
        • 嗯,问题是:我怎么知道哪些设备是wifi接口。您的列表仅提供通过NetworkInterface.getNetworkInterfaces() 并寻找以wlan 开头的设备。但这已经在 OP 中了。
        • 是的,这样回答它可以帮助他找出“只有 wifi 接口”。 Thatsy 我从 wlan 开始写的
        • 您的回答提供了完全 OP 已经拥有的相同信息:以wlan 开头的网络设备的ipv4 地址。
        【解决方案4】:

        我最近发现 WifiAP ip 地址在 Android 中是硬编码的。除非用户手动更改了这个值(我认为这很不常见),否则使用硬编码值是绝对足够的。我认为这是最好的方法。 IP 地址为“192.168.43.1”:https://github.com/CyanogenMod/android_frameworks_base/blob/cm-10.1/wifi/java/android/net/wifi/WifiStateMachine.java?source=c#L1299

        【讨论】:

        • 对我不起作用(在 HTC One 上)。当我设置 Wifi 热点时,我的手机获得了 IP 192.168.1.1。我通过从我的 Windows 框连接到热点并通过“ipconfig /all”检查 Wi-Fi 网关 IP 发现了这一点。
        【解决方案5】:

        尝试一下这段代码:

        public String getLocalIpAddress() {
                try {
                    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                        NetworkInterface intf = en.nextElement();
                        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                            InetAddress inetAddress = enumIpAddr.nextElement();
                            if (!inetAddress.isLoopbackAddress()) {
        
                                String ip = Formatter.formatIpAddress(inetAddress.hashCode());
                                Toast.makeText(getApplicationContext(), "***** IP="+ ip, 1).show();
        
                                return ip;
                            }
                        }
                    }
                } catch (SocketException ex) {
                    Toast.makeText(getApplicationContext(), "***** IP="+ex.toString(), 1).show();
        
                }
                return null;
            }
        

        在oncreat写代码检查,热点是Enable are not.....

        WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
                wifiManager.setWifiEnabled(true);
                wifiManager.setWifiEnabled(false);
                boolean wifiEnabled = wifiManager.isWifiEnabled();
                if (wifiEnabled) {
                    Toast.makeText(getApplicationContext(),"on",Toast.LENGTH_SHORT).show();
                    getLocalIpAddress();
                }else {
                        Toast.makeText(getApplicationContext(),"off",Toast.LENGTH_SHORT).show();
                }
        

        【讨论】:

          【解决方案6】:

          一开始我尝试获取WiFi接口的MAC地址,并与每个接口的MAC地址进行比较。但事实证明,至少在我运行 CM 的 N4 上,WiFi 接口的 MAC 在打开 Hotspot 时会发生变化。

          所以我编写了一些代码来遍历设备列表以找到识别 wifi 接口的内容。此代码在我的 N4 上完美运行:

          private String getWifiIp() throws SocketException {
              for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
                      en.hasMoreElements(); ) {
                  NetworkInterface intf = en.nextElement();
                  if (intf.isLoopback()) {
                      continue;
                  }
                  if (intf.isVirtual()) {
                      continue;
                  }
                  if (!intf.isUp()) {
                      continue;
                  }
                  if (intf.isPointToPoint()) {
                      continue;
                  }
                  if (intf.getHardwareAddress() == null) {
                      continue;
                  }
                  for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                          enumIpAddr.hasMoreElements(); ) {
                      InetAddress inetAddress = enumIpAddr.nextElement();
                      if (inetAddress.getAddress().length == 4) {
                          return inetAddress.getHostAddress();
                      }
                  }
              }
              return null;
          }
          

          只有一个接口符合所有条件:wlan0

          其他可能的解决方案:

          浏览一些最常见的接口名称并尝试在列表中找到它们:new String[] { "wlan0", "eth0", ...];

          【讨论】:

          • 即使我的 hotsopt 处于活动状态,这也会引发套接字异常。
          【解决方案7】:

          请将intf.getName()替换为intf.getDisplayName(),然后尝试。

          【讨论】:

          • 引用文档“返回此网络接口的人类可读名称。在 Android 上,这与 getName() 返回的字符串相同。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-09-28
          • 1970-01-01
          • 2015-02-09
          • 2011-09-01
          • 2018-05-19
          • 1970-01-01
          • 2012-03-31
          相关资源
          最近更新 更多