Android代码实例如何实现wifi连接
程序员文章站
2022-07-05 22:19:48
首先加入权限。 ...
首先加入权限。
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
然后新建一个wifi工具类。
public class WifiUtils {
private static WifiUtils utils = null;
public WifiUtils(Context context){
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
public static WifiUtils getInstance(Context context){
if (utils == null){
synchronized (WifiUtils.class){
if (utils == null){
utils = new WifiUtils(context);
}
}
}
return utils;
}
private WifiManager wifiManager;
/**
* wifi是否打开
* @return
*/
public boolean isWifiEnable(){
boolean isEnable = false;
if (wifiManager != null){
if (wifiManager.isWifiEnabled()){
isEnable = true;
}
}
return isEnable;
}
/**
* 打开WiFi
*/
public void openWifi(){
if (wifiManager != null && !isWifiEnable()){
wifiManager.setWifiEnabled(true);
}
}
/**
* 关闭WiFi
*/
public void closeWifi(){
if (wifiManager != null && isWifiEnable()){
wifiManager.setWifiEnabled(false);
}
}
/**
* 有密码连接
* @param ssid
* @param pws
*/
public void connectWifiPws(String ssid, String pws){
wifiManager.disableNetwork(wifiManager.getConnectionInfo().getNetworkId());
int netId = wifiManager.addNetwork(getWifiConfig(ssid, pws, true));
wifiManager.enableNetwork(netId, true);
}
/**
* wifi设置
* @param ssid
* @param pws
* @param isHasPws
*/
private WifiConfiguration getWifiConfig(String ssid, String pws, boolean isHasPws){
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + ssid + "\"";
WifiConfiguration tempConfig = isExist(ssid);
if(tempConfig != null) {
wifiManager.removeNetwork(tempConfig.networkId);
}
if (isHasPws){
config.preSharedKey = "\""+pws+"\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}else {
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
return config;
}
/**
* 得到配置好的网络连接
* @param ssid
* @return
*/
private WifiConfiguration isExist(String ssid) {
List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
if (config.SSID.equals("\""+ssid+"\"")) {
return config;
}
}
return null;
}
}
假设ssid和pw信息格式如下。
private String mString = "ssid:123,pw:12332";
private String ssid;
private String pw;
private WifiUtils mWifiUtils;
需使用正则表达式获取ssid和pw。
Pattern p1= Pattern.compile("ssid:(\\w+),pw:");
Matcher m1 = p1.matcher(mString);
while(m1.find()){
ssid= m1.group(1);
Log.d(TAG,"ssid = " + ssid);
}
Pattern p2= Pattern.compile("pw:(\\w+)");
Matcher m2 = p2.matcher(mString);
while(m2.find()){
pw = m2.group(1);
Log.d(TAG,"pw = " + pw);
}
最后调用connectWifiPws
去连接wifi。
mWifiUtils = WifiUtils.getInstance(this);
mWifiUtils.openWifi();
mWifiUtils.connectWifiPws(ssid,pw);
参考链接:
JAVA 截取两段字符串中间的值
Android连接WiFi
本文地址:https://blog.csdn.net/sinat_24353711/article/details/107760760