How to get each device's IP address in Wi-Fi Direct scenario?

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.

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());

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.

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.

Good luck...


The best answer you can get is possibly the one from Mano:

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.

Here is how I implemented it. When I connect a client to the group owner via WiFi Direct, I get the group owner's ip address, and send a message to the group owner over a socket. Something like:

Socket socket = new Socket();
socket.setReuseAddress(true);
socket.connect((new InetSocketAddress(mIP, mPort)), SOCKET_TIMEOUT);
OutputStream os = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(new String("BROFIST");
oos.close();
os.close();
socket.close();

You already know mIP (the group owner's IP address), and only have to decide a mPort and receive the connection on the group owner like this:

Socket serverSocket = new ServerSocket(mPort);
serverSocket.setReuseAddress(true);
Socket client = serverSocket.accept();
ObjectInputStream objectInputStream = new ObjectInputStream(client.getInputStream());
Object object = objectInputStream.readObject();
if (object.getClass().equals(String.class) && ((String) object).equals("BROFIST")) {
  Log.d(TAG, "Client IP address: "+client.getInetAddress());
}

This is the actual code I'm using. I'm going to replace this message with some useful info, like a message object containing the MAC of the sender, which can be used to know about MAC-IP relations, since WifiP2pDevice only provides MAC and InetAddress the IP (Does anyone know if there's a way to get the MAC from an InetAddress object?)


I have done a demo project that is able to get each device ip and send data from one device to another (be the group owner or not). The url is:

https://github.com/ahmontero/wifi-direct-demo

I hope it helps you!

EDIT: Basically looking up the IP address in ARP cache like this:

public static String getIPFromMac(String MAC) {
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {

            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4) {
                // Basic sanity check
                String device = splitted[5];
                if (device.matches(".*p2p-p2p0.*")){
                    String mac = splitted[3];
                    if (mac.matches(MAC)) {
                        return splitted[0];
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

I've been able to connect and send both ways in a WiFi direct network. All devices know the group owner's IP Address. We get the peer IP from a socket created for communication. Somehow getting the IP from the socket at the server side (at the group owner) did not work for me. So, I send the group owner the IP of the individual device from that device itself. To get the IP simply create a socket and bind it to any local port and get the address from this socket and send it over to the owner. I've used an UDP socket but it works well enough with TCP sockets.

DatagramSocket socket=null;
     try {
        socket=new DatagramSocket();
        socket.connect((new InetSocketAddress(host, port)));
        String address=socket.getLocalAddress().getHostAddress();
        ByteBuffer bb=ByteBuffer.allocate(2+address.length());
        bb.putChar('I');
        bb.put(address.getBytes());
        DatagramPacket pkt=new DatagramPacket(bb.array(),2+address.length());
        socket.send(pkt);
        Log.d(WiFiDirectActivity.TAG,"address"+address+"dest"+host);
        Log.d(WiFiDirectActivity.TAG,"send");
    } catch (SocketException e) {
        Log.e(WiFiDirectActivity.TAG,"error socketException");
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (socket != null) {
            if (socket.isConnected()) {
                socket.close();
            }
    }

host is the address of the group owner we get from the connection info.