Seducer_star
Seducer_star
  • 发布:2015-07-24 10:37
  • 更新:2017-10-15 10:45
  • 阅读:5353

Android连接指定SSID的WIFI热点

分类:Native.js

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();  
            }  

谢谢 磊子哥的帮助 ,分享

2015-07-24 10:37 负责人:无 分享
已邀请:
chrishon

chrishon

请问链接后如何通信,比如往ap发信息

houjie10241015@163.com

houjie10241015@163.com - 关注wifi连接

楼主你好,请问如何判断需要连接的wifi的加密方式,也就是你的type如何确定?

ynzu

ynzu

我把代码拷贝过去,调试的时候没有报错,指定wifi连不上,请问是什么原因?

该问题目前已经被锁定, 无法添加新回复