Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Wi-Fi Direct Network

I am developing an application on Android where I am searching for all the peers in the range and afterwards connect with all of them, The device who initiated the the discovery become the group owner and all others become client, I have done all the connection thing but now I want to the group owner to send the message to all the connecting peers, How to achieve this and also please tell me what is the methodology in peer-to-peer communication , Does p2p in Android also use IP to send and receive data?

Thankyou Regards Talib.

like image 278
Talib Avatar asked Oct 05 '13 11:10

Talib


People also ask

Is Wi-Fi Direct available for Android?

Wi-Fi Direct is a peer-to-peer Wi-Fi connection between devices that enables faster data transfer than Bluetooth with lower latency than Wi-Fi through a router. You can use it on Android with the Cast Screen feature on certain devices and the Nearby Share file-sharing feature.

How does Wi-Fi Direct work on Android?

How Does Wi-Fi Direct Work? Wi-Fi Direct doesn't require a centralized network or wireless router to share information between devices. Instead, when a connection is made, one device acts as the access point or hotspot. Other devices then connect to this original device using WPS and WPA/WPA2 protocols.


1 Answers

Wi-Fi Direct/P2P can be considered as normal Wi-Fi but where the group owner (GO) acts as a software access point (dhcp server, provisioning, etc). So to answer your last question, yes Wi-Fi Direct also uses IP to send and receive data.

You want to send data to all members in the group? There are two solutions for this:

  1. Broadcast the message once using multicast.
  2. Send the message to each individuel client in the group.

The most efficient method would be solution 1, to broadcast the data using multicast, as you would only need to send the data once. Unfortunately Wi-Fi multicast support is very fragmented in Android, as a lot of devices seem to block non-unicast traffic. See this article for more in depth information if you want to go down this route.

Solution 2 is the best method if you want to guarantee support on all devices and only transmit a small amount of data. The GO need the IP addresses of the clients in the group, but because of the way Wi-Fi Direct is implemented in Android, only the GO IP is known to all devices. One solution is to let the clients connect to a socket on the GO, to get their IP address:

Client code

private static final int SERVER_PORT = 1030;

... // on group join:
wifiP2pManager.requestConnectionInfo(channel, new ConnectionInfoListener() {
    @Override
    public void onConnectionInfoAvailable(WifiP2pInfo p2pInfo) {
        if (!p2pInfo.isGroupOwner) {
            // Joined group as client - connect to GO
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress(p2pInfo.groupOwnerAddress, SERVER_PORT));
        }
    }
});

Group owner code:

private static final int SERVER_PORT = 1030;
private ArrayList<InetAddress> clients = new ArrayList<InetAddress>();

public void startServer() {
    clients.clear();
    ServerSocket serverSocket = new ServerSocket(SERVER_PORT);

    // Collect client ip's
    while(true) {
       Socket clientSocket = serverSocket.accept();
       clients.add(clientSocket.getInetAddress());
       clientSocket.close();
    }
}

Now all you need to do is start a serversocket on each client, and make to GO iterate through the client list creating a socket connection to each and sending the message you want to broadcast.

like image 120
Nikki Ashton Avatar answered Sep 20 '22 22:09

Nikki Ashton