Android开启WiFi热点后如何获取自己本身的IP地址?
ringa_lee
用下这个方法,可以获取到你的开启hotpspot之后的ip
public String getWifiApIpAddress() { try { 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)) { Log.d(TAG, inetAddress.getHostAddress()); return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { Log.e(TAG, ex.toString()); } return null; }
ringa_lee