import WifiManager from "android.net.wifi.WifiManager"; import WifiConfiguration from 'android.net.wifi.WifiConfiguration'; function isHexWepKey(wepKey:String):boolean { let len = wepKey.length(); // WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?) if (len != 10 && len != 26 && len != 58) { return false; } return isHex(wepKey); } function isHex(key:string):boolean { for (var i = key.length() - 1; i >= 0; i--) { let c = key.charAt(i); if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f')) { return false; } } return true; } // 查看以前是否也配置过这个网络 function isExsits(SSID:string ,wifiManager:WifiManager):WifiConfiguration | null{ let existingConfigs = wifiManager.getConfiguredNetworks(); for (let existingConfig in existingConfigs) { if (existingConfig.SSID.equals("\" + SSID + "\")) { return existingConfig; } } return null; } function createWifiInfo(SSID:string ,password:string):WifiConfiguration { let config = new WifiConfiguration(); config.allowedAuthAlgorithms.clear(); config.allowedGroupCiphers.clear(); config.allowedKeyManagement.clear(); config.allowedPairwiseCiphers.clear(); config.allowedProtocols.clear(); config.SSID = "\"" + SSID + "\""; // // nopass // // if (Type == WifiCipherType.WIFICIPHER_NOPASS) { // // config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); // // } // // // wep // // if (Type == WifiCipherType.WIFICIPHER_WEP) { // if (!TextUtils.isEmpty(Password)) { // if (isHexWepKey(Password)) { // config.wepKeys[0] = Password; // } else { // config.wepKeys[0] = "\"" + Password + "\""; // } // } // config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); // config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED); // config.allowedKeyManagement.set(KeyMgmt.NONE); // config.wepTxKeyIndex = 0; // // } // // wpa // if (Type == WifiCipherType.WIFICIPHER_WPA) { // config.preSharedKey = "\"" + Password + "\""; // 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.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); // config.allowedPairwiseCiphers // .set(WifiConfiguration.PairwiseCipher.CCMP); // config.status = WifiConfiguration.Status.ENABLED; // } return config; } class ConnectRunnable extends Runnable { ssid:string = "" password:string = "" constructor(ssid:string,password:string) { this.ssid = ssid this.password = password } override run():void{ try { // 打开wifi wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING WifiConfiguration wifiConfig = createWifiInfo(ssid, password, type); WifiConfiguration tempConfig = isExsits(ssid); if (tempConfig != null) { wifiManager.removeNetwork(tempConfig.networkId); } int netID = wifiManager.addNetwork(wifiConfig); boolean enabled = wifiManager.enableNetwork(netID, true); boolean connected = wifiManager.reconnect(); } catch (Exception e) { // TODO: handle exception sendMsg(e.getMessage()); e.printStackTrace(); } } } class WifiConnector { wifiManager:WifiManager; //WIFICIPHER_WEP是WEP ,WIFICIPHER_WPA是WPA,WIFICIPHER_NOPASS没有密码 // enum WifiCipherType { // WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID // } // 构造函数 constructor(wifiManager:WifiManager) { this.wifiManager = wifiManager; } // 提供一个外部接口,传入要连接的无线网 connect(ssid:string ,password:string) { Thread thread = new Thread(new ConnectRunnable(ssid,password)); thread.start(); } }