2012-07-13 83 views
2

我想解決下一個問題。我的設備處於AP模式(便攜式WiFi熱點)。 It has to show IP of it。另一個設備通過使用已知的IP連接到這個設備。它必須在沒有任何WiFi路由器的情況下工作,只需設備到設備。 如果無線電已經在AP模式下運行,如何獲取IP地址?我有一些關於AP的代碼:Android - 獲取IP,如果WiFi已經在AP模式下運行

public boolean setWifiApEnabled(WifiConfiguration config, boolean enabled) {   
     try { 
     if (enabled) { // disable WiFi in any case 
     mWifiManager.setWifiEnabled(false); 
     } 

     Method method = mWifiManager.getClass().getMethod(
      "setWifiApEnabled", WifiConfiguration.class, 
      boolean.class); 
     return (Boolean) method.invoke(mWifiManager, config, enabled); 
     } catch (Exception e) { 
     //Log.e(TAG, "", e); 
     return false; 
     } 
     } 

public int getWifiApState() { 
     try { 
     Method method = mWifiManager.getClass().getMethod(
      "getWifiApState"); 
     return (Integer) method.invoke(mWifiManager); 
     } catch (Exception e) { 
     //Log.e(TAG, "", e); 
     return WIFI_AP_STATE_FAILED; 
     } 
     } 

public static boolean IsWifiApEnabled(Context context){ 
      boolean isWifiAPEnabled = false;   
      WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
      Method[] wmMethods = wifi.getClass().getDeclaredMethods(); 
      for(Method method: wmMethods){ 
       if(method.getName().equals("isWifiApEnabled")) { 
        try { 
        isWifiAPEnabled = (Boolean) method.invoke(wifi); 
        } catch (IllegalArgumentException e) { 
        e.printStackTrace(); 
        } catch (IllegalAccessException e) { 
        e.printStackTrace(); 
        } catch (InvocationTargetException e) { 
        e.printStackTrace(); 
        } 
       } 
      } 
      return isWifiAPEnabled; 
     } 
} 

也許有一些技巧可以得到它的IP?請幫幫我。謝謝。

+0

我認爲你的AP IP是192.168.1.1 – 2012-11-08 13:46:27

+0

你爲什麼認爲我的AP IP是192.168.1.1?這是不對的!我的AP是192.168.43.1!我只是枚舉所有的網絡接口,並找到它! – Nolesh 2012-11-09 00:06:20

回答

1

我用這個來確定IP地址:

private String determineHostAddress() { 
     try { 
      for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); nis.hasMoreElements();) { 
       NetworkInterface ni = nis.nextElement(); 
       if (!ni.isLoopback() && !ni.isVirtual() && ni.isUp() && !ni.isPointToPoint() && ni.getHardwareAddress() != null) { 
        for (Enumeration<InetAddress> ips = ni.getInetAddresses(); ips.hasMoreElements();) { 
         InetAddress ip = ips.nextElement(); 
         if (ip.getAddress().length == 4) { 
          return ip.getHostAddress(); 
         } 
        } 
       } 
      } 
     } catch (Exception ignored) {} 
     return null; 
    } 
-1

這可能不是你的情況下適用,但在我的應用我正打算以顯示熱點的IP地址,這樣我就可以在另一個進入它Android設備連接到熱點以連接到在熱點上運行的網絡服務器。在這種情況下,連接到熱點的客戶端(連接到熱點的設備)可以簡單地查詢它連接到的網關的IP地址。這將始終是熱點的IP地址。這裏是我的代碼:

@SuppressWarnings("deprecation") // Deprecated because it doesn't handle IPv6 but wifimanager only supports IPV4 anyway 
private String getGateway() 
{ 
    final WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE); 
    DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); 
    return Formatter.formatIpAddress(dhcpInfo.gateway); 
} 

順便說一句,在每一個Android的我已經測試到目前爲止熱點的IP地址始終192.168.43.1。根據this question,它在Android源代碼中被硬編碼。

相關問題