安卓 - 连接已知隐藏的Wi-Fi网络

4

我需要以编程方式连接到隐藏的Wi-Fi网络。我知道它的SSID、安全类型和密码。但出于某些原因,我无法连接它。

如果它没有被隐藏,我可以连接到同一个网络。

这是我的代码:

// configure the network
private void saveWPANetwork(WiFiNetwork network){     
    WifiConfiguration conf = new WifiConfiguration(); 
    conf.SSID =network.getSSID(); 
    conf.hiddenSSID = true; 
    conf.status = WifiConfiguration.Status.ENABLED; 
    conf.preSharedKey =network.getPassword(); 
    conf.priority = 9999; 
    wifi.addNetwork(conf); 
    wifi.saveConfiguration(); 
}

// connect it
protected boolean connectToVaildNetwork() { 

    List<WifiConfiguration> list = wifi.getConfiguredNetworks(); 
    if(list == null) 
        return false; 

    for( WifiConfiguration i : list ) { 
        for (WiFiNetwork network : config.wiFiNetworksDetails) { 
            if(network.getSSID().equalsIgnoreCase(i.SSID)){ 
                wifi.enableNetwork(i.networkId, true); 
                return wifi.reconnect(); /// STRANGE BUT IT ALWAYS RETURNS TRUE, EVEN IF DEVICE IS NOT CONNECTED TO THE  HIDDEN NETWORK! 
            } 
        } 
    } 
    return false; 
}

1个回答

8
这个答案可能有些晚了,但我还是发帖以便有需要的人参考。 已经在Android 4.4.2上测试过。值得注意的是隐藏网络连接需要更长的时间(在我的测试中大约为10-15秒)。
wifi.reconnect() == true 表示您的命令已经成功请求,但并不意味着wifi已经连接。
public void setWifiConfig(String ssid, String sharedKey) {
    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + ssid + "\"";   // Please note the quotes. String should contain ssid in quotes

    conf.preSharedKey = "\"" + sharedKey + "\"";

    conf.hiddenSSID = true;
    conf.status = WifiConfiguration.Status.ENABLED;
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

    wifiManager.addNetwork(conf);

    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
    for (WifiConfiguration i : list) {
        if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {

            wifiManager.disconnect();

            wifiManager.enableNetwork(i.networkId, true);

            wifiManager.reconnect();

            wifiManager.saveConfiguration();
            break;
        }
    }
}

1
谢谢,你的答案对我有用。顺便提一下,我的设备是基于Android 5.1.1的。 - Yuan
2
我已经在Android 7上尝试过,但无法使其工作,不知道它是否适用于新的API? - caiomcg
你可以尝试使用 conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); - Anh-Tuan Mai
没有成功:我刚刚打印了i的值,得到了这个结果。可能是什么原因呢?
  • DSBLE ID: 66 SSID:“myHiddenSSID” PROVIDER-NAME: null BSSID: null FQDN: null PRIO: 783 numAssociation 11 validatedInternetAccess KeyMgmt: WPA_PSK Protocols: WPA RSN AuthAlgorithms: OPEN PairwiseCiphers: TKIP CCMP GroupCiphers: TKIP CCMP PSK: * 企业配置: 密码* 引擎0 客户端证书NULL 匿名身份NULL ca_cert2 NULL 身份* 域后缀匹配NULL phase2 NULL altsubject_match NULL 主题匹配NULL ca_cert NULL key_id NULL engine_id NULL eap NULL
- Usman
你需要更新一些代码行,使其像这样: int networkId = mWifiManager.addNetwork(conf); 而在if语句中应该像这样: if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"") && networkId == i.networkId)你需要检查networkId,以防用户已经保存了与隐藏网络相同名称的网络。 - Akef

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接