How many devices can i connect with Wi-Fi P2P?
Asked Answered
A

3

7

I need to connect 20+ android devices in a client-server network. Each client Android device will be communicating with the server Android device and vice versa. The client devices do not need to communicate with each other. The server device would need access to internet for a brief period while connected to the clients.

My question is, can Wi-Fi P2P support that many connections reliably? And if yes, how do I go about implementing them? Or will I have to ensure that all devices are on the same WLAN?

Audry answered 24/4, 2016 at 7:30 Comment(4)
How would you initiate wifi p2p between two android devices? Or between two apps on two android devices? What do you have in mind with wifi p2p?Lila
@Lila : I was hoping to follow the android docs here and use the sample code to build upon. I am new to network programming. The devices need to be connected to send instructions between client-server based on which the UI will be updated.Audry
I will not follow your link. I expect you to post a readable understandable text here.Lila
@Lila : Sorry, am new to using stack overflow. The link is http://developer.android.com/guide/topics/connectivity/wifip2p.htmlAudry
C
10

From experience, in a real-world deployment of an Android Wi-Fi Direct application, 20 devices should not be an issue.

Theoretically, the maximum number of devices in a Wi-Fi P2P group, where the GO is an Android device, is 254. The group owner is assigned the IP, 192.168.49.1. Clients are assigned an IP from the range, 192.168.49.2 to 192.168.49.254.

The group owner address is defined by the following in WifiP2pServiceImpl.java:

/* Is chosen as a unique address to avoid conflict with
   the ranges defined in Tethering.java */
private static final String SERVER_ADDRESS = "192.168.49.1";

Determining the range for the clients is done as follows:

In WifiP2pServiceImpl.java, the startDhcpServer(String intf) method will start the DHCP server for a given interface - not a surprise. This method is called when the group has started and the device is the group owner.

Taking a closer look at this code, we can see that on the InterfaceConfiguration object, the link address is set to 192.168.49.1 and the prefix length is 24 (prefix length is the number of bits set in a subnet mask, here equivalent to 255.255.255.0) - this implies the answer, but we can dig a little further.

ifcg = mNwService.getInterfaceConfig(intf);
ifcg.setLinkAddress(new LinkAddress(NetworkUtils.numericToInetAddress(
                    SERVER_ADDRESS), 24));
ifcg.setInterfaceUp();
mNwService.setInterfaceConfig(intf, ifcg);

Next, the following commands will restart tethering with the DHCP range specified by the String[], tetheringDhcpRanges. The calls of mNwService (Network Management Service) methods will execute the appropriate netd commands.

ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(
    Context.CONNECTIVITY_SERVICE);
String[] tetheringDhcpRanges = cm.getTetheredDhcpRanges();
if (mNwService.isTetheringStarted()) {
    if (DBG) logd("Stop existing tethering and restart it");
    mNwService.stopTethering();
}
mNwService.tetherInterface(intf);
mNwService.startTethering(tetheringDhcpRanges);

And cm.getTetheredDhcpRanges() is ultimately a reference to the following (ConnectivityManager.getTetheredDhcpRanges() -> ConnectivityService.getTetheredDhcpRanges() -> Tethering.getTetheredDhcpRanges()):

// USB is  192.168.42.1 and 255.255.255.0
// Wifi is 192.168.43.1 and 255.255.255.0
// BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
// with 255.255.255.0
// P2P is 192.168.49.1 and 255.255.255.0

private String[] mDhcpRange;
private static final String[] DHCP_DEFAULT_RANGE = {
    "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
    "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
    "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
    "192.168.48.2", "192.168.48.254", "192.168.49.2", "192.168.49.254",
}

and:

mDhcpRange = context.getResources().getStringArray(
    com.android.internal.R.array.config_tether_dhcp_range);
if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) {
    mDhcpRange = DHCP_DEFAULT_RANGE;
}

in com.android.server.connectivity.Tethering.

Of course, it is possible for the device manufacturer to change this code, so this is also worth considering.

For those planning to deploy applications where there will be many users, a mechanism to allow a more than one device to be GO is required. If data needs to be synchronised between devices, it is simple to simulate "churn" and have GOs only be a GO for a time period before becoming a client to another GO and synchronising any data.

Cinthiacintron answered 4/5, 2016 at 8:51 Comment(0)
S
1

The max number as far as I know is not specified, so you would need to test that out to be certain. Also there could be differences between hardware.

Anyway, the basic implementation would be rather simple. The server would call GreateGroup, so it would be the Groupowner in all cases. And then start locals service advertising. Clients then would simply look for the advertisement and once they see it, they would start connection process to the server. One the server connection is made over Wifi direct you would simply start socket communications from the client to the server (server would have listening socket on all times).

Note that connection would require user to click on the dialog showed when client tries to connect to the group owner. And if you want to get rid of this. Then you could actually use the Accesspoint created by GreateGroup, and add the access point name as well as the password to the advertising. Then your clients could actually use the accesspoint to connect (like to any Wlan accesspoint)

Note though that the Wifi Direct way, would not interfere with Wifi connections, not would it require it. But the accesspoint way would mean that any existing Wifi connection from the client would be disconnected, and the device thinks that the connection made to the server would provide normal internet connectivity.

Stidham answered 27/4, 2016 at 8:38 Comment(0)
C
0

Remember that devices don't need to be connected to a network to connect to each other. Wi-Fi Direct allows them to connect directly.

Here is a list of Wi-Fi Direct resources that you may find useful: https://groups.google.com/forum/#!topic/wi-fi-direct/uWpuOzHY6y0

I'd recommend following Android's Service Discovery Demo and try implementing it yourself. And here is the source code for the demo.

Checkbook answered 27/4, 2016 at 1:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.