上次写了Android链接匿名WiFi的内容。WiFI开发对于应用层开发是比较小众的知识点,不过既然用到了就在此记录下。java
一、根据加密类型、密码、是否隐藏等参数来建立热点android
static WifiConfiguration createWifiConfig(String SSID, @WifiSecurityType int wifiCipherType, String password, boolean hiddenSSID) {
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = convertToQuotedString(SSID);
wifiConfiguration.hiddenSSID=hiddenSSID;//是否隐藏热点true=隐藏,若是隐藏须要其余设备手动添加网络
switch (wifiCipherType) {
case WifiSecurityType.SECURITY_NONE:
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
break;
case WifiSecurityType.SECURITY_WEP:
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
wifiConfiguration.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
wifiConfiguration.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
if (!TextUtils.isEmpty(password)) {
int length = password.length();
// WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
if ((length == 10 || length == 26 || length == 58)
&& password.matches("[0-9A-Fa-f]*")) {
wifiConfiguration.wepKeys[0] = password;
} else {
wifiConfiguration.wepKeys[0] = '"' + password + '"';
}
}
break;
case WifiSecurityType.SECURITY_WPA_PSK:
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
if (!TextUtils.isEmpty(password)) {
if (password.matches("[0-9A-Fa-f]{64}")) {
wifiConfiguration.preSharedKey = password;
} else {
wifiConfiguration.preSharedKey = '"' + password + '"';
}
}
break;
case WifiSecurityType.SECURITY_WPA_EAP:
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
wifiConfiguration.enterpriseConfig = new WifiEnterpriseConfig();
int eapMethod = 0;
int phase2Method = 0;
wifiConfiguration.enterpriseConfig.setEapMethod(eapMethod);
wifiConfiguration.enterpriseConfig.setPhase2Method(phase2Method);
if (!TextUtils.isEmpty(password)) {
wifiConfiguration.enterpriseConfig.setPassword(password);
}
break;
default:
break;
}
return wifiConfiguration;
}
复制代码
而后调用WifiManager的setWifiApEnabled
方法来设置wifiConfiguration
,由于是隐藏的,须要经过反射:bash
try {
Method method = mWifManager.getClass().getMethod(
"setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
boolean enable = (Boolean) method.invoke(mWifManager, config, true);
if (enable) {
Log.d("WiFi", "热点已开启");
} else {
Log.d("WiFi", "建立热点失败");
}
} catch (Exception e) {
e.printStackTrace();
}
复制代码
关闭热点比较简单,也是用上面的方法,第二个参数传false就好了:网络
public void closeWifiAp() {
try {
Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(mWifiManager, null, false);
} catch (Exception e) {
e.printStackTrace();
}
}
复制代码
热点的状态能够经过广播的方式来监听:加密
public static final String WIFI_AP_STATE_CHANGED_ACTION =
"android.net.wifi.WIFI_AP_STATE_CHANGED";
复制代码
不过这个变量是隐藏的,只能直接经过值来注册广播:spa
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.wifi.WIFI_AP_STATE_CHANGED");
复制代码
而后在广播中获取state:.net
int state = intent.getIntExtra("wifi_state", 0);
复制代码
wifi热点有以下几种状态:code
#WIFI_AP_STATE_DISABLED
#WIFI_AP_STATE_DISABLING
#WIFI_AP_STATE_ENABLED
#WIFI_AP_STATE_ENABLING
#WIFI_AP_STATE_FAILED
复制代码
获取WiFI热点当前状态,返回值就是上面五种状态:ip
public int getWifiApState() 复制代码
判断WiFi热点是否打开:开发
public boolean isWifiApEnabled() 复制代码
获取当前wifi热点的WifiConfiguration
:
public WifiConfiguration getWifiApConfiguration() 复制代码