WIFI连接

1 首先,要实现一个简单的WIFI连接设置,我们需要掌握和WIFI功能相关的一些类,比如WIfiManager,WifiInfo,ScanResult,WifiConfiguration等,提供了WIFI连接相关的基本的API.
  
比如:
  
打开关闭网关:wifiManager.setWifiEnabled(true/false);
  
扫描周边网络:wifiManager.getScanResults();
  
连接指定网络:wifiManager.enableNetwork(networkId,true);
  
添加网络:wifiManager.addNetwork(wcg);
  
移除网络:wifiManager.removeNetwork(netId);
  
获取网卡状态:wifiManager.getWifiState()
  
……
  
2 扫描的网络将会被保存在一个List<ScanResult>中,同时WifiManager会为我们维护一个List<WifiConfiguration>,这个List中保存了我们已经连接过的配置好的网络连接.
  
当我们选择一个网络时,判断它是否存在于这个List中,存在即可直接连接,否则需要用户输入密码创建一个新的WifiConfiguration.
  
3 获得的ScanResult中将会保存有该无线连接的相关信息,包括SSID,BSSID,capabilities,level等属性,其中SSID号是该连接的一个标识符,比如我们经常看到的TP_LINKXXX.
  
capabilities中保存了相关加密信息,比如WEB和WPA等.level则表示信号度.
  
4 在获取连接状态时,即调用wifiManager.getWifiState()或者wifiInfo.getSupplicantState()时,通 常在用户已经授权成功后,我们获得的状态值就为COMPLETED,此时不管网络是否已经连接成功,我们都无法获得新的状态. 所以要判断WIFI网络是否已经真的连接成功需要用到如下方法判断:
  ConnectivityManager connManager = (ConnectivityManager) WifiConnection.this
              .getSystemService(CONNECTIVITY_SERVICE);
 // 获取代表联网状态的NetWorkInfo对象
 NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
  
if(networkInfo != null && networkInfo.getType() == 1
 && wifiAdmin.getWifiInfo().getSSID()!=null)
 {
  
 
//WIFI网络连接成功
  
}
  
5 获取本地IP地址的方法:
  
public String getLocalIpAddress() {    
        try {    
            for (Enumeration<NetworkInterface> en = NetworkInterface    
                    .getNetworkInterfaces(); en.hasMoreElements();) {    
                NetworkInterface intf = en.nextElement();    
                for (Enumeration<InetAddress> enumIpAddr = intf    
                        .getInetAddresses(); enumIpAddr.hasMoreElements();) {    
                    InetAddress inetAddress = enumIpAddr.nextElement();    
                    if (!inetAddress.isLoopbackAddress()) {    
                        return inetAddress.getHostAddress().toString();    
                    }    
                }    
            }    
        } catch (SocketException ex) {    
            Log.e("WifiPreference IpAddress", ex.toString());    
        }    
        return null;    
    }    
  
 
6 在创建一个新的WifiConfiguration时,切记SSID和preSharedKey必须添加双引号,否则必将会导致连接失败.正确写法如下:
 //创建一个新的WifiConfiguration
 WifiConfiguration wcg = new WifiConfiguration();
 wcg.BSSID = mBSSID;
 //SSID和preSharedKey必须添加双引号,否则将会导致连接失败
 wcg.SSID = """ + mSSID + """;
 
wcg.hiddenSSID = false;
 wcg.status = WifiConfiguration.Status.ENABLED;
 wcg.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
 wcg.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
 wcg.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
 wcg.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
 wcg.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
 wcg.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
 //如果加密模式为WEP
 if(mSecurity.equals("WEP"))
 {
    wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    wcg.wepKeys[0] =""" + editText.getText().toString() + """; //This is the WEP Password
    wcg.wepTxKeyIndex = 0;
 }
 //如果加密模式为WPA EPA
 else if(mSecurity.equals("WPA EAP"))
 {
 wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
 wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
 wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
 wcg.preSharedKey = """ + editText.getText().toString() + """;
 }
 //如果加密模式为WPA PSK
 else if(mSecurity.equals("WPA PSK"))
 {
 wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
 wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
 wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
 wcg.preSharedKey = """ + editText.getText().toString() + """;
 }
 //无加密
 else
 {
 wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
 }
原文地址:https://www.cnblogs.com/olvo/p/3039546.html