android 设置默认wifi,[Android][Framework]设置默认WiFi配置

本文深入解析Android系统中如何设置默认的WiFi热点配置,从TetherSettings到WifiApDialog,再到WifiServiceImpl和WifiStateMachine,一步步揭示配置过程。通过mWifiConfig获取并填充对话框内容,最后在WifiApConfigStore中使用getDefaultApConfiguration生成包含默认SSID和WPA2_PSK安全性的配置。
摘要由CSDN通过智能技术生成

点击Wifi热点配置选项,会弹出一个对话框,WifiApDialog.java。这个对话框会在onCreate的时候填入一些默认内容:

if (mWifiConfig != null) {

mSsid.setText(mWifiConfig.SSID);

if (mWifiConfig.apBand == 0) {

mBandIndex = 0;

} else {

mBandIndex = 1;

}

mSecurity.setSelection(mSecurityTypeIndex);

if (mSecurityTypeIndex == WPA2_INDEX) {

mPassword.setText(mWifiConfig.preSharedKey);

}

}

内容都是从mWifiConfig获取的,这个mWifiConfig是在构造的时候赋值:

// TetherSettings.java

private void initWifiTethering() {

final Activity activity = getActivity();

mWifiConfig = mWifiManager.getWifiApConfiguration(); // 这里获取的

mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);

mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);

设置WiFi静态模式,您需要在Android 10的framework代码中进行以下步骤: 1. 打开WifiManager.java文件并添加以下代码: ``` /** * Set the IP address, gateway, and subnet mask for a static IP configuration. * * @param ipAddress the IP address in integer form * @param gateway the gateway in integer form * @param subnetMask the subnet mask in integer form * @param dns the DNS server addresses in integer form * @return {@code true} if the operation succeeded, {@code false} otherwise * @hide */ public boolean setWifiStaticIpConfiguration(int ipAddress, int gateway, int subnetMask, int[] dns) { WifiConfiguration config = getCurrentWifiConfiguration(); if (config == null) { return false; } config.setIpAssignment(IpAssignment.STATIC); config.setIpAddress(InetAddress.getByAddress(intToByteArray(ipAddress)).getHostAddress()); config.setGateway(InetAddress.getByAddress(intToByteArray(gateway)).getHostAddress()); config.setNetmask(InetAddress.getByAddress(intToByteArray(subnetMask)).getHostAddress()); config.setDns(dns); return saveWifiConfiguration(config); } private byte[] intToByteArray(int value) { return new byte[] { (byte)(value >>> 24), (byte)(value >>> 16), (byte)(value >>> 8), (byte)value}; } ``` 2. 在WifiConfiguration.java文件中,添加以下代码: ``` /** * Set the IP assignment mode. * * @param ipAssignment the IP assignment mode to set * @hide */ public void setIpAssignment(IpAssignment ipAssignment) { mIpAssignment = ipAssignment; } /** * Set the IP address. * * @param ipAddress the IP address to set * @hide */ public void setIpAddress(String ipAddress) { mIpAddress = ipAddress; } /** * Set the gateway. * * @param gateway the gateway to set * @hide */ public void setGateway(String gateway) { mGateway = gateway; } /** * Set the subnet mask. * * @param netmask the subnet mask to set * @hide */ public void setNetmask(String netmask) { mNetmask = netmask; } /** * Set the DNS server addresses. * * @param dns the DNS server addresses to set * @hide */ public void setDns(int[] dns) { mDnsServers.clear(); for (int i = 0; i < dns.length; i++) { mDnsServers.add(InetAddress.getByAddress(intToByteArray(dns[i]))); } } ``` 3. 最后,在Settings app中添加WiFi的静态IP配置打开WifiSettings.java文件并添加以下代码: ``` /** * Show the dialog for configuring a static IP address. * * @param config the WifiConfiguration to configure * @hide */ public void showWifiStaticIpConfigDialog(WifiConfiguration config) { StaticIpConfigDialogFragment fragment = StaticIpConfigDialogFragment.newInstance(config); fragment.show(getFragmentManager(), StaticIpConfigDialogFragment.TAG); } ``` 这些代码将允许您在Android 10中设置WiFi的静态IP地址。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值