2014-02-15 48 views
10

我需要在主持熱點時查找設備的IP地址。我目前使用此代碼:Android在主持熱點時查找設備的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名在某些設備上的不同。所以我必須首先找到該設備的名稱(爲其熱點)。我怎樣才能找到這個名字?還是有更好的方法來找到設備的IP地址?

///查找由MAC正確的IP地址也似乎不工作

回答

8

最近,我想通了,在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

+1

沒有爲我工作(在HTC One上)。當我設置一個Wifi熱點時,我的手機的IP爲192.168.1.1。我通過連接到Windows盒子的熱點並通過'ipconfig/all'檢查Wi-Fi網關IP,發現了這一點。 –

0

intf.getDisplayName()取代intf.getName(),然後嘗試。

+0

引用自文檔「返回此網絡接口的人類可讀名稱**在Android上,這與getName()返回的字符串相同**」 – flx

11

起初我試着去取WiFi接口的MAC地址,把它與每個接口的MAC地址進行比較。但事實證明,至少在運行CM的N4上,打開熱點時,WiFi接口的MAC會發生變化。

因此,我寫了一些代碼來通過設備列表來查找某些內容以識別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

其它可能的解決方案:

走線槽一些最常用的接口的名字,並試圖找到它們在列表中:在oncreat

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; 
    } 

寫代碼:new String[] { "wlan0", "eth0", ...];

+0

感謝代碼共享。它拯救了我的一天。 –

+2

這個答案應該是被接受的答案 –

+0

即使我的hotsopt處於活動狀態,這仍然會引發套接字異常。 – 2016-10-02 17:11:21

0

一旦試試這個代碼檢查,熱點啓用不是.....

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(); 
     } 
2

這可以幫助你。

製作的外殼打電話,要求網絡適配器和檢查,你需要像在這種情況下是無線局域網的那些所以按照這個代碼

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(); 

附加註釋

This image is the output of the command on the shell. 或者您可以使用Play商店中的Android Terminal Emulator來在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)); 
} 
+0

這不提供任何好處。它只緩存名爲'wlanX'的設備。有設備命名那裏wifi接口'ethX'。 – flx

+0

如果你需要別人,你只需要刪除正則表達式中的wlan,但是如果你正確讀取它,他需要wifi接口的名稱。所以我很喜歡在它裏面提到wlan。 – cafebabe1991

+0

好吧,問題是:我如何知道**哪些設備是wifi接口**。您的列表僅提供了通過'NetworkInterface.getNetworkInterfaces()'並查找以'wlan'開頭的設備。但是這已經在OP中。 – flx

1

此解決方案時,你想要得到的熱點設備的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(); 
    } 


      } 
0

user2224350說:「我最近想通了,在WifiAP IP地址採用的是Android硬編碼。」

除非用戶手動更改此值(我認爲這種情況非常罕見),但使用硬編碼值是絕對足夠的。我認爲這是最好的選擇。 IP地址是「192.168.43.1」,as seen here

這適用於我的三星A3和華爲PRA-LX1,但HTC Desires 530也返回192.168.1.1