Android开启WiFi热点时App可以监听设备本身的IP吗?
ringa_lee
ringa_lee 2017-04-17 17:56:24
0
1
486

Android开启WiFi热点后如何获取自己本身的IP地址?

ringa_lee
ringa_lee

ringa_lee

reply all(1)
PHPzhong

Use this method to get your IP after opening hotpspot

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;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template