I've got a problem using wifi direct. I managed to connect 2 devices and send data from the client to the group owner, because the group owner ip is the one that everybody knows. I managed also to find out the IP of the client and pass it to the group owner but I can't send data from the group owner to the client, even if it should be simmetric. I'm using Intent
and startService()
to send data and AsynkTask
for receive. Using only 2 devices I noticed that the client IP is always the same (192.168.49.10), so I give it to the intent manually.
Here's the method where I'm trying to create the sender for the owner and the receiver 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 should start the service to send the data, but the service never starts. How I already said, the same service starts if it's used on the client side and the data is transferred correctly from the client to the owner.