【问题标题】:How to get the IP address of a non-group owner in WiFi Direct?如何在 WiFi Direct 中获取非群组所有者的 IP 地址?
【发布时间】:2012-10-18 03:05:51
【问题描述】:

如果提议连接的设备被指定为组所有者,我们如何知道其他设备的 IP 地址?我们可以得到群主的IP,但我不知道如何获取非群主的IP。因为请求连接的不是设备,所以它没有 WifiP2pInfo 类。它甚至不知道群主的IP。如何将数据从这台设备发送给群组所有者?

提前致谢!

【问题讨论】:

  • 你得到答案了吗????如果是,请分享,我需要它。

标签: ip wifi android-wifi wifi-direct


【解决方案1】:

您可以获取两个对等方的本地 IP 地址,然后将它们与组所有者 IP 进行比较。您可能已经知道,您可以通过这行代码轻松获取组所有者 IP:

WifiP2pInfo.info.groupOwnerAddress.getHostAddress();

对于本地 IP,您可以简单地使用:

localIp = getDottedDecimalIP(getLocalIPAddress());

使用以下相关方法:

private byte[] getLocalIPAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface
                .getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf
                    .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    if (inetAddress instanceof Inet4Address) {
                        return inetAddress.getAddress();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        // Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
    } catch (NullPointerException ex) {
        // Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
    }
    return null;
}

private String getDottedDecimalIP(byte[] ipAddr) {
    if (ipAddr != null) {
        String ipAddrStr = "";
        for (int i = 0; i < ipAddr.length; i++) {
            if (i > 0) {
                ipAddrStr += ".";
            }
            ipAddrStr += ipAddr[i] & 0xFF;
        }
        return ipAddrStr;
    } else {
        return "null";
    }
}

【讨论】:

  • 谢谢。连接套接字时,我通过 socket.getInetAddress() 获得了 IP。
  • 在我测试时不适用于三星 Galaxy Grand 2。无法通过这种方式获取 ip。
猜你喜欢
  • 1970-01-01
  • 2013-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多