WifiConnector.uts 4.0 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
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();
    }

   

    


    
}