Android Wifi Direct: how to send data from the group owner to customers?

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.

+2
source share
1 answer

As Laszlo Magyar said, you first need to send an empty message to the server so that the server can use the client socket to receive the incoming IP address.

My solution is to send a string to the server from the client so that the server can know the IP address of the client, and the rest of the process is the same.

+1
source

Source: https://habr.com/ru/post/1204695/


All Articles