search

Home  >  Q&A  >  body text

Android开启WiFi热点时App可以监听设备本身的IP吗?

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

ringa_leeringa_lee2773 days ago489

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-17 17:58:24

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

    reply
    0
  • Cancelreply