android wifi(二)

黄弘盛
2023-12-01

wifi ip的两种分配方式为STATIC和DHCP,通过WifiConfiguration.ipAssignment == IpAssignment.STATIC和WifiConfiguration.ipAssignment == IpAssignment.DHCP来判断。

获取一个已保存的AP的详细信息:ip地址,mask掩码,gateway网关,DNS。注意当ip分配方式为static时的获取掩码方式。

public WifiDetailInfo getWifiDetailInfo(WifiConfiguration configuration, boolean isConnected) {
		WifiDetailInfo detailInfo = new WifiDetailInfo();
		detailInfo.isAutoIp = (configuration.ipAssignment == IpAssignment.DHCP);
		detailInfo.name = configuration.SSID;
		//当此wifi已连接
		if (isConnected) {
			DhcpInfo mDhcpInfo = mWifiManager.getDhcpInfo();
			detailInfo.ip = getAddress(mDhcpInfo.ipAddress);
			detailInfo.gateway = getAddress(mDhcpInfo.gateway);
			detailInfo.dns = getAddress(mDhcpInfo.dns1);
			detailInfo.mask = getAddress(mDhcpInfo.netmask);
			//当当前wifi的ip分配方式为static时,通过DhcpInfo获得的掩码为0,必须通过以下方式获取
			if (mDhcpInfo.netmask == 0) {
				LinkProperties localLinkProperties;
				localLinkProperties = configuration.linkProperties;
				Iterator localIterator1 = localLinkProperties.getLinkAddresses().iterator();

				if (localIterator1.hasNext()) {
					LinkAddress localLinkAddress = (LinkAddress) localIterator1.next();

					//获得网络的前缀长度,然后根据前缀长度来获取掩码
					int i = localLinkAddress.getNetworkPrefixLength();
					if (i > 0 && i <= 32) {
						detailInfo.mask = getAddress(NetworkUtils.prefixLengthToNetmaskInt(i));
					}
				}
			}
		}
		//当此wifi未连接
		else {
			LinkProperties localLinkProperties;
			localLinkProperties = configuration.linkProperties;
			Iterator<LinkAddress> localIterator1 = localLinkProperties.getLinkAddresses().iterator();

			if (localIterator1.hasNext()) {
				LinkAddress localLinkAddress = (LinkAddress) localIterator1.next();
				detailInfo.ip = localLinkAddress.getAddress().getHostAddress();
			}
		}
		return detailInfo;
	}

当要改变ip的分配方式时,根据当前的WifiConfiguration获取一个新的WifiConfiguration。

private WifiConfiguration getNewConfig(WifiConfiguration currentConfig) {
		WifiConfiguration config = new WifiConfiguration();
		config.networkId = currentConfig.networkId;
		if (currentConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
			config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
		} else if (currentConfig.allowedKeyManagement.get(KeyMgmt.WPA_EAP)
				|| currentConfig.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
			config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
			config.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
		}
		if (currentConfig.wepKeys[0] == null) {
			config.allowedKeyManagement.set(KeyMgmt.NONE);
		} else {
			config.allowedKeyManagement.set(KeyMgmt.NONE);
			config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
			config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
		}
		return config;
	}

根据新的WifiConfiguration设置ip分配方式,以及新的ip,网关,dns来设置LinkProperties。

private void setIpFields(WifiConfiguration config, LinkProperties linkProperties, String ip, String gateway,
			String dns, boolean isAutoIp) {
		// is auto ip
		if (isAutoIp) {
			config.ipAssignment = IpAssignment.DHCP;
		} else {
			config.ipAssignment = IpAssignment.STATIC;
			// config linkProperties

			try {
				InetAddress inetIp = NetworkUtils.numericToInetAddress(ip);
				linkProperties.addLinkAddress(new LinkAddress(inetIp, 24));

				InetAddress inetGateway = NetworkUtils.numericToInetAddress(gateway);
				linkProperties.addRoute(new RouteInfo(inetGateway));

				InetAddress inetDns1 = NetworkUtils.numericToInetAddress(dns);
				linkProperties.addDns(inetDns1);

			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			}
		}
	}

最后就可以调用mWifiManager.save(newConfig, listener);方法来配置ip分配方式。

public void setIp(WifiConfiguration config, String ip, String gateway, String dns, boolean isAutoIp,
			ActionListener listener) {
		WifiConfiguration newConfig = getNewConfig(config);
		if (newConfig != null) {
			LinkProperties linkProperties = new LinkProperties();
			setIpFields(newConfig, linkProperties, ip, gateway, dns, isAutoIp);
			newConfig.linkProperties = new LinkProperties(linkProperties);
			mWifiManager.save(newConfig, listener);
			updateAccessPoints();
		}
	}



 类似资料: