【问题标题】:Android - connect to known hidden Wi-Fi networkAndroid - 连接到已知的隐藏 Wi-Fi 网络
【发布时间】:2015-08-12 09:20:06
【问题描述】:

我需要以编程方式连接到隐藏的 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; 
}

【问题讨论】:

    标签: android wifi hidden


    【解决方案1】:

    这个答案可能迟了,但我仍然会发布它以防有人需要。 安卓 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;
            }
        }
    }
    

    【讨论】:

    • 谢谢,你的答案对我有用。仅供参考,我的设备基于 Android 5.1.1
    • 我已经在 android 7 上尝试过,但无法让它工作,知道它是否可以在新的 API 上工作吗?
    • 你可以试试conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    • 没有成功:我刚刚打印了 i 的值,得到了这个。可能是什么原因? - DSBLE ID: 66 SSID: "myHiddenSSID" PROVIDER-NAME: null BSSID: null FQDN: null PRIO: 783 numAssociation 11 validInternetAccess KeyMgmt: WPA_PSK 协议: WPA RSN AuthAlgorithms: OPEN PairwiseCiphers: TKIP CCMP GroupCiphers: TKIP CCMP PSK: * Enterprise config : 密码 * 引擎 0 client_cert NULL anonymous_identity NULL ca_cert2 NULL identity * domain_suffix_match NULL phase2 NULL altsubject_match NULL subject_match NULL ca_cert NULL key_id NULL engine_id NULL eap NULL
    • 您需要将代码的一些行更新为:int networkId = mWifiManager.addNetwork(conf);,并且在 if 语句中应如下所示:if (i.SSID != null &amp;&amp; i.SSID.equals("\"" + ssid + "\"") &amp;&amp; networkId == i.networkId) 您需要检查 networkId,以防用户已经有一个与隐藏网络同名的已保存网络。
    猜你喜欢
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 2014-11-19
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多