I have a problem using wifi direct. I managed to connect 2 devices and send data from the client to the owner of the group, because the owner of the ip group is the one that everyone knows. I also managed to find out the IP address of the client and transfer it to the owner of the group, but I can not send data from the owner of the group to the client, even if it should be symmetric. I use Intent and startService() to send data and AsynkTask to receive. Using only 2 devices, I noticed that the client IP address is always the same (192.168.49.10), so I give it the intention manually.
Here is the method where I try to create a sender for the owner and recipient for the client:
@Override public void onConnectionInfoAvailable(final WifiP2pInfo info) { // InetAddress from WifiP2pInfo struct. InetAddress groupOwnerAddress = info.groupOwnerAddress; connected = true; ownerIP = groupOwnerAddress.getHostAddress(); // After the group negotiation, we can determine the group owner. if (info.groupFormed && info.isGroupOwner) { Toast.makeText(MainActivity.this, "I'm the owner!!", Toast.LENGTH_SHORT).show(); owner = true; // Do whatever tasks are specific to the group owner. // One common case is creating a server thread and accepting // incoming connections. Intent serviceIntent = new Intent(MainActivity.this, OwnerSender.class); serviceIntent.setAction(OwnerSender.ACTION_SEND); serviceIntent.putExtra(OwnerSender.EXTRAS_CLIENT_ADDRESS,"192.168.49.10"); serviceIntent.putExtra(OwnerSender.EXTRAS_CLIENT_PORT, 8988); startService(serviceIntent); //new OwnerReceiver(this).execute(); // owner riceve dai client sulla porta 8988 } else if (info.groupFormed) { // The other device acts as the client. In this case, // you'll want to create a client thread that connects to the group // owner. /*Intent serviceIntent = new Intent(MainActivity.this, ClientSender.class); serviceIntent.setAction(ClientSender.ACTION_SEND); serviceIntent.putExtra(ClientSender.EXTRAS_GROUP_OWNER_ADDRESS,ownerIP); serviceIntent.putExtra(ClientSender.EXTRAS_GROUP_OWNER_PORT, 8988); startService(serviceIntent);*/ new ClientReceiver(this).execute(); // i client ricevono dall'owner sula porta 8989 Toast.makeText(MainActivity.this, "I'm a client...", Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, "Server IP: " + groupOwnerAddress.getHostAddress(), Toast.LENGTH_SHORT).show(); } }
This method starts when the connection is established and the owner must start the service to send data, but the service never starts. As I said, the same service is launched if it is used on the client side, and the data is transferred correctly from the client to the owner.
source share