根据经验,在实际部署 Android Wi-Fi Direct 应用程序时,20 台设备应该不是问题。
理论上,一个 Wi-Fi P2P 组(GO 是 Android 设备)中的最大设备数为 254。组所有者分配了 IP 192.168.49.1。为客户端分配了一个范围为 192.168.49.2 到 192.168.49.254 的 IP。
群主地址在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";
确定客户端的范围如下:
在WifiP2pServiceImpl.java 中,startDhcpServer(String intf) 方法将为给定接口启动 DHCP 服务器 - 这并不奇怪。当组已启动并且设备是组所有者时调用此方法。
仔细看这段代码,我们可以看到在InterfaceConfiguration对象上,链接地址设置为192.168.49.1,前缀长度为24(前缀长度为子网掩码中设置的位数,这里相当于 255.255.255.0) - 这意味着答案,但我们可以进一步挖掘。
ifcg = mNwService.getInterfaceConfig(intf);
ifcg.setLinkAddress(new LinkAddress(NetworkUtils.numericToInetAddress(
SERVER_ADDRESS), 24));
ifcg.setInterfaceUp();
mNwService.setInterfaceConfig(intf, ifcg);
接下来,以下命令将使用 String[] tetheringDhcpRanges 指定的 DHCP 范围重新启动网络共享。 mNwService(网络管理服务)方法的调用将执行相应的 netd 命令。
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);
而cm.getTetheredDhcpRanges() 最终是对以下内容的引用(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",
}
和:
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;
}
在com.android.server.connectivity.Tethering.
当然,设备制造商可以更改此代码,因此这也值得考虑。
对于那些计划在会有很多用户的地方部署应用程序的人,需要一种允许多台设备运行的机制。如果需要在设备之间同步数据,可以很简单地模拟“流失”,让 GO 在成为另一个 GO 的客户端并同步任何数据之前只作为一个 GO 一段时间。