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?)