Wi-Fi Direct场景下如何获取每台设备的IP地址? [英] How to get each device's IP address in Wi-Fi Direct scenario?

查看:37
本文介绍了Wi-Fi Direct场景下如何获取每台设备的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从ICS开始,引入了Wi-Fi Direct.正常情况下,我们使用WifiP2pManager类对Wi-Fi Direct进行操作,但似乎只有连接后才能检索GroupOwner IP地址.但是,实际上,任何设备都经过协商成为 GroupOwner.在上层应用中,我们需要获取peer的IP地址,或者一个组中每个peer的IP地址,以便我们可以与它们发送/通信.

Starting from ICS, Wi-Fi Direct is introduced. Normally, we use the WifiP2pManager class to operate on Wi-Fi Direct, but it seems that it can only retrieve the GroupOwner IP address after connected. But, actually, any device all came negotiate to become the GroupOwner. In Upper Application, we need to get the peer's IP address, or each peer's IP address in a group so that we can send/communicate with them.

如何获取 Wi-Fi Direct 中的每个 IP 地址?包括自己的IP地址和组中的每个对等点?

How to get each IP address in Wi-Fi Direct? Include own IP address and each peer in the group?

推荐答案

我遇到了同样的问题.由于两个设备都知道群主的ip,所以已经可以向群主发送消息了.您发送的第一条消息可以包含其他设备的IP地址;从此,双向通信成为可能.

I encountered the same problem. Since both devices know the group owner's ip, it is already possible to send a message to the group owner. The first message you send can contain the ip address of the other device; from then on, bidirectional communication is possible.

这是在java中检索您的IP的可能性:

Here's a possibility for retrieving your ip in java:

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) { // fix for Galaxy Nexus. IPv4 is easy to use :-) 
                        return inetAddress.getAddress(); 
                    } 
                    //return inetAddress.getHostAddress().toString(); // Galaxy Nexus returns IPv6 
                } 
            } 
        } 
    } catch (SocketException ex) { 
        //Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex); 
    } catch (NullPointerException ex) { 
        //Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex); 
    } 
    return null; 
}

private String getDottedDecimalIP(byte[] ipAddr) {
    //convert to dotted decimal notation:
    String ipAddrStr = "";
    for (int i=0; i<ipAddr.length; i++) {
        if (i > 0) {
            ipAddrStr += ".";
        }
        ipAddrStr += ipAddr[i]&0xFF;
    }
    return ipAddrStr;
}

ip = getDottedDecimalIP(getLocalIPAddress());

将此 ip 包装在一个 Serializable 对象中,并将其发送给组所有者,就像发送任何其他对象一样.将此视为您的 wifi-direct 协议中的第一步...现在,群组所有者也有一个 IP 可以向其发送答案.

Wrap this ip in a Serializable object and send it to the group owner like you would send any other object. Consider this the first step in your wifi-direct protocol... Now, the group owner also has an IP to send answers to.

这对我有用,尽管我认为我必须自己实现它很奇怪,而且我只能轻松找到组所有者 ip (info.groupOwnerAddress.getHostAddress();//with info a WifiP2pInfo instance).也许有类似的方法可以检索其他同行的ip,但我找不到.如果有,请联系我.

This works for me, although I think it's weird that I had to implement this myself and I could only find the group owner ip easily (info.groupOwnerAddress.getHostAddress(); //with info a WifiP2pInfo instance). Maybe there is a comparable way to retrieve the ip of the other peers, but I couldn't find it. Please contact me if you do.

祝你好运...

这篇关于Wi-Fi Direct场景下如何获取每台设备的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆