簡體   English   中英

在Android Emulator上獲取WIFI網絡中的計算機的IP地址

[英]get Ip address of computers in WIFI network on Android Emulator

我想以編程方式查找通過WiFi連接到Android設備或模擬器的計算機的IP地址。 我該怎么做呢?

你可以共享logcat,我懷疑可能還有其他問題。在示例應用程序中嘗試使用此代碼(按原樣),僅檢查Wi-Fi IP地址是否正常工作

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();

int ipAddress = wifiInfo.getIpAddress();

String ip = null;

ip = String.format("%d.%d.%d.%d",
(ipAddress & 0xff),
(ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff),
(ipAddress >> 24 & 0xff))

如果要檢測連接到任何網絡的“仿真器”或Android設備的IP地址,請在您的程序中使用此代碼。 它將為您提供網絡為您的設備分配的確切IP地址。

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= inetAddress.getHostAddress().toString();
                               //Now use this Ip Address...
                         }   
                       }
                  }

            }
     catch (SocketException obj) 
     { 
       Log.e("Error occurred during IP fetching: ", obj.toString());
      }

如另一個主題所述,android Emulator在虛擬專用網絡上運行。

這意味着模擬器與計算機不在同一網絡上,而是在虛擬模擬器上。 沒有模擬器可以看到其他設備,也沒有其他模擬器,也沒有其他設備可以看到模擬器。

除此之外我還有一個問題:

如何使用WifiManager獲取主機名的IP地址?

例如,我的PC與我的Android手機(不是模擬器)在同一個局域網上,它有一個像User-PC這樣的主機名。 當我嘗試使用InetAddress.getByName(“User-PC”)獲取IP時; 在java應用程序上,我得到的局域網IP如192.168.1.100,但是當我在手機上嘗試它時它不起作用。奇怪的是我可以建立連接,如果我知道IP,但似乎無法解決它主機名。

有任何想法嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM