function ConnectWifi(ssid, keyword, type, level) {

var SSID = ssid;

var Password = keyword;

var Context = plus.android.importClass("android.content.Context");

var WifiManager = plus.android.importClass("android.net.wifi.WifiManager");

var wifiManager = plus.android.runtimeMainActivity().getSystemService(Context.WIFI_SERVICE);

var WifiConfiguration = plus.android.importClass('android.net.wifi.WifiConfiguration');

var config = new WifiConfiguration();

if (type == 1) {

var allowedKeyManagement = plus.android.getAttribute(config, 'allowedKeyManagement');

plus.android.importClass(allowedKeyManagement);

allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

}

if (type == 2) {

plus.android.setAttribute(config, 'wepKeys[0]', "\"" + Password + "\"");

plus.android.setAttribute(config, 'hiddenSSID', true);

var allowedAuthAlgorithms = plus.android.getAttribute(config, 'allowedAuthAlgorithms');

plus.android.importClass(allowedAuthAlgorithms);

allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);

var allowedGroupCiphers = plus.android.getAttribute(config, 'allowedGroupCiphers');

plus.android.importClass(allowedGroupCiphers);

allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);

allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);

var allowedKeyManagement = plus.android.getAttribute(config, 'allowedKeyManagement');

plus.android.importClass(allowedKeyManagement);

allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

config.wepTxKeyIndex = 0;

}

if (type == 3) {

plus.android.setAttribute(config, 'SSID', "\"" + SSID + "\"");

plus.android.setAttribute(config, 'preSharedKey', "\"" + Password + "\"");

plus.android.setAttribute(config, 'hiddenSSID', true);

var allowedAuthAlgorithms = plus.android.getAttribute(config, 'allowedAuthAlgorithms');

plus.android.importClass(allowedAuthAlgorithms);

allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);

var allowedGroupCiphers = plus.android.getAttribute(config, 'allowedGroupCiphers');

plus.android.importClass(allowedGroupCiphers);

allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

var allowedKeyManagement = plus.android.getAttribute(config, 'allowedKeyManagement');

plus.android.importClass(allowedKeyManagement);

allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

var allowedPairwiseCiphers = plus.android.getAttribute(config, 'allowedPairwiseCiphers');

plus.android.importClass(allowedPairwiseCiphers);

allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);

var allowedGroupCiphers = plus.android.getAttribute(config, 'allowedGroupCiphers');

plus.android.importClass(allowedGroupCiphers);

allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);

var allowedPairwiseCiphers = plus.android.getAttribute(config, 'allowedPairwiseCiphers');

plus.android.importClass(allowedPairwiseCiphers);

allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

plus.android.setAttribute(config, 'status', WifiConfiguration.Status.ENABLED);

}

var wcgID = wifiManager.addNetwork(config);

var b = wifiManager.enableNetwork(wcgID, true);

//plus.webview.currentWebview().reload();

}

谢谢 磊子哥的帮助 ,分享

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐