android 设置wifi连接

1.new 一个wificonfiguration对象。

2.设置这个对象的一些属性。

WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\""+sr.SSID+"\"";      //这个地方一定要注意了。旁边的“是不能够省略的。密码的地方也一样。
wc.preSharedKey = "\""+etPassword.getText().toString()+"\"";      //该热点的密码
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

3.判断wifi是否加密:

    public static int getSecurity(ScanResult result) {
        if (result.capabilities.contains("WEP")) {
            return 1;
        } else if (result.capabilities.contains("PSK")) {
            return 2;
        } else if (result.capabilities.contains("EAP")) {
            return 3;
        }
        return 0;
    }
    

4.连接未加密wifi连接:

WifiConfiguration config = new WifiConfiguration();
config.SSID = "\"" + sr.SSID + "\"";
config.allowedKeyManagement.set(KeyMgmt.NONE);
int networkId = wifiManager.addNetwork(config);
if(networkId != -1){
    wifiManager.enableNetwork(networkId, false);
    wifiManager.saveConfiguration();


上一篇:ML之SVM:利用SVM算法对手写数字图片识别数据集(PCA降维处理)进行预测并评估模型(两种算法)性能


下一篇:数据库,主键为何不宜太长长长长长长长长?