Android Wifi Direct: How to send data from Group Owner to the clients?

懵懂的女人 提交于 2019-12-30 07:24:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/32359071/android-wifi-direct-how-to-send-data-from-group-owner-to-the-clients

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!