直接上代码:
package com.realtop.commonutils.utils;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.net.IpConfiguration;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiEnterpriseConfig;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class WIFIHelper {
public static final String TAG = "wifi_utils";
public static String getForgetName(WifiManager manager) {
WifiInfo connectionInfo = manager.getConnectionInfo();
String ssid = connectionInfo.getSSID().replace("\"", "");
return ssid;
}
public static void forgetNetwork(WifiManager manager) {
try {
WifiInfo connectionInfo = manager.getConnectionInfo();
String ssid = connectionInfo.getSSID().replace("\"", "");
int networkId = connectionInfo.getNetworkId();
Log.i(TAG, "forgetNetwork: enter:" + ssid + "; " + networkId);
manager.forget(networkId, new WifiManager.ActionListener() {
@Override
public void onSuccess() {
Log.i(TAG, "forgetNetwork: onSuccess: is ok");
manager.removeNetwork(networkId);
}
@Override
public void onFailure(int reason) {
Log.i(TAG, "forgetNetwork: onFailure: reason:" + reason);
manager.removeNetwork(networkId);
}
});
ComUtils.saveString(ComConfig.WIFI_PWD_PRE + ssid, "");
Log.i(TAG, "forgetNetwork: remove id:" + networkId + ";" + ssid);
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "forgetNetwork: error:" + e.getMessage());
}
}
/**
* 连接wifi
*
* @param manager WifiManager
* @param configuration Wifi配置
* @return 是否连接成功
*/
public static boolean connectWifi(WifiManager manager, WifiConfiguration configuration) {
Log.i(TAG, "connectWifi: enter");
manager.connect(configuration, new WifiManager.ActionListener() {
@Override
public void onSuccess() {
Log.i(TAG, "connectWifi: onSuccess: connect ok");
}
@Override
public void onFailure(int reason) {
Log.i(TAG, "connectWifi: onFailure: error:" + reason);
}
});
return true;
}
/**
* 创建Wifi配置
*
* @param SSID wifi名称
* @param password wifi密码
* @param hidden 网络是否隐藏(该方法与添加隐藏网络通用)
* @param capabilities 网络安全协议
* @return 配置好的wifi
*/
public static WifiConfiguration createWifiInfo(String SSID, String password, boolean hidden, String capabilities) {
WifiConfiguration configuration = new WifiConfiguration();
configuration.SSID = "\"" + SSID + "\"";
if (hidden) {
configuration.hiddenSSID = true;
}
Log.d("WifiManagerUtils", "createWifiInfo: " + capabilities);
if (capabilities.contains("SAE") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
setWPA3(configuration, password);
} else if (capabilities.contains("WPA-PSK") || capabilities.contains("WPA2-PSK")) {
setWPA(configuration, password);
} else if (capabilities.contains("WEP")) {
setWEP(configuration, password);
} else {
setESS(configuration);
}
return configuration;
}
@SuppressLint("NewApi")
public static WifiConfiguration createWifiConfiguration(String SSID, String password, boolean hidden, String capabilities) {
WifiConfiguration config = new WifiConfiguration();
config.SSID = "\"" + SSID + "\"";
config.hiddenSSID = hidden;
config.shared = false;
Log.d("WifiManagerUtils", "createWifiInfo: " + capabilities);
// 分别适配安全类型
if (capabilities.contains("SAE")) {
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE);
if (!TextUtils.isEmpty(password)) {
config.preSharedKey = '"' + password + '"';
}
Log.i(TAG, "createWifiConfiguration: sae");
} else if (capabilities.contains("WPA-PSK") || capabilities.contains("WPA2-PSK")) {
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK);
if (!TextUtils.isEmpty(password)) {
if (password.matches("[0-9A-Fa-f]{64}")) {
config.preSharedKey = password;
} else {
config.preSharedKey = '"' + password + '"';
}
}
Log.i(TAG, "createWifiConfiguration: psk");
} else if (capabilities.contains("WEP")) {
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_WEP);
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]*")) {
config.wepKeys[0] = password;
} else {
config.wepKeys[0] = '"' + password + '"';
}
}
Log.i(TAG, "createWifiConfiguration: wep");
} else if (capabilities.contains("OWE")) {
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OWE);
Log.i(TAG, "createWifiConfiguration: owe");
} else if (capabilities.contains("EAP")) {
setEAP(config, password);
Log.i(TAG, "createWifiConfiguration: eap");
} else {
// 无密码
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OPEN);
config.shared = true;
Log.i(TAG, "createWifiConfiguration: open");
}
final IpConfiguration ipConfig = new IpConfiguration();
ipConfig.setIpAssignment(IpConfiguration.IpAssignment.UNASSIGNED);
ipConfig.setProxySettings(IpConfiguration.ProxySettings.UNASSIGNED);
ipConfig.setStaticIpConfiguration(null);
ipConfig.setHttpProxy(null);
config.setIpConfiguration(ipConfig);
return config;
}
private static void setEAP(WifiConfiguration wifiConfig, String password) {
// 设置身份验证类型为 EAP
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
// 设置 EAP 方法(这是具体的 EAP 身份验证类型)
wifiConfig.enterpriseConfig.setEapMethod(-1);
// 设置身份验证用户名和密码
wifiConfig.enterpriseConfig.setIdentity("");
wifiConfig.enterpriseConfig.setPassword(password);
}
/**
* 设置wpa3协议
*
* @param configuration 配置
* @param password 密码
*/
public static void setWPA3(WifiConfiguration configuration, String password) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.SAE);
}
configuration.preSharedKey = "\"" + password + "\"";
}
/**
* WPA协议
*
* @param configuration 配置
* @param password 密码
*/
public static void setWPA(WifiConfiguration configuration, String password) {
configuration.preSharedKey = "\"" + password + "\"";
//公认的IEEE 802.11验证算法。
configuration.allowedAuthAlgorithms.clear();
configuration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
//公认的的公共组密码。
configuration.allowedGroupCiphers.clear();
configuration.allowedGroupCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
//公认的密钥管理方案。
configuration.allowedKeyManagement.clear();
configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//密码为WPA。
configuration.allowedPairwiseCiphers.clear();
configuration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
configuration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
//公认的安全协议。
configuration.allowedProtocols.clear();
configuration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
}
/**
* WEP协议
*
* @param configuration 配置
* @param password 密码
*/
public static void setWEP(WifiConfiguration configuration, String password) {
configuration.wepKeys[0] = "\"" + password + "\"";
configuration.wepTxKeyIndex = 0;
configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
}
/**
* 无密码
*
* @param configuration 配置
*/
public static void setESS(WifiConfiguration configuration) {
configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
public static List<BluetoothDevice> getBlueConnectDevices() {
BluetoothManager bluetoothManager = (BluetoothManager) EventBusHelper.getContext()
.getSystemService(Context.BLUETOOTH_SERVICE);
@SuppressLint("MissingPermission")
List<BluetoothDevice> connectedDevices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
if (connectedDevices != null) {
return connectedDevices;
} else {
return new ArrayList<>();
}
}
}