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號是該連接的一個標識符,比如我們經??吹降腡P_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/archive/2013/04/24/3039546.html
總結
- 上一篇: 文档源代码opencv 中的错误erro
- 下一篇: flash怎么描图案轮廓?flash图案