2012-07-13 84 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源代码中被硬编码。

相关问题