2

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
glorfindel
  • 199
  • 3
  • 11
  • After the clients connected, they have to send a message or something to the group owner via socket communication (they know the group owner IP). The group owner can read the client IP from the socket. Store the IP, and you can send data to the clients. – László Magyar Sep 03 '15 at 06:48
  • At first I tried to send data from the client to the owner and it worked fine. I stored the IP of the client and tried to send data from the owner to the client, but it didn't work. I noticed that with 2 devices the IP of the client is always the same (192.168.49.10). In the example above I'm trying to send directly from the owner to the client after the connection is established. Since the IP of the client is always the same, I give it to the service directly, but it doesn't work in any case. – glorfindel Sep 03 '15 at 07:33
  • The thing I don't get is why the service doesn't want to start. I'm pretty sure that the IP of the client is correct, and the port (8988) is the same I used to send data from the client to the owner. – glorfindel Sep 03 '15 at 07:33
  • 1
    @MatteoBernardon found something? – David Sep 26 '15 at 18:02
  • Nothing news. Maybe the problem is unsolvable. – glorfindel Sep 28 '15 at 11:34
  • 1
    @MatteoBernardon Hey, did you resolve this issue ? – Ihor Bykov Jan 10 '16 at 13:09

1 Answers1

2

Like Laszlo Magyar said, you need to sent an empty message to server first so that server can use client socket to get the incoming ip address.

My solution is to send an string to server from client so server CAN know the client's ip address, and the rest of process is the same.

Lady Chocobo
  • 55
  • 11