【问题标题】:How to remember wifi configuration and connected network through-out the reboots如何在重启过程中记住 wifi 配置和连接的网络
【发布时间】:2012-03-18 06:54:42
【问题描述】:

我正在使用以下代码创建新的 wifi 接入点并连接到它。
这段代码工作正常,我可以连接到 wifi 接入点,但我面临的问题是我正在创建的 wifi 连接在设备重启后没有被记住。

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"SSIDName\"";
wc.preSharedKey  = "\"password\"";
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;        
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.RSN);
int res = wifi.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = wifi.enableNetwork(res, true);        
Log.d("WifiPreference", "enableNetwork returned " + b );

我想要存档的是当我成功连接到 SSID 时,我想记住该网络,并且在下次重新启动设备时,Android 应该自动连接到之前连接到的 SSID。

WifiManagerWifiConfiguration 中是否有任何 API 可以这样做?

谢谢。

【问题讨论】:

  • 是不是WifiManager.saveConfiguration()保存了当前创建的wifi配置。

标签: android android-wifi


【解决方案1】:

我们必须通过调用 WifiManager.saveConfiguration() 来保存创建的 wifi 配置,这会保存当前创建的 wifi 配置,我们还需要为创建的 wifi 配置设置最高优先级,以便在下次重启时 android wi-fi 管理器优先这个网络。

【讨论】:

  • WifiManager.saveConfiguration() 已弃用
【解决方案2】:

编写一个广播接收器,为每个引导时间设置用户名和密码。此时不要编写任何 UI。

【讨论】:

    【解决方案3】:

    试试这个 WPA 代码:

            WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            WifiConfiguration wc = new WifiConfiguration(); 
            wc.SSID = "\""+SSIDname+"\""; //IMP! This should be in Quotes!!
            wc.hiddenSSID = false;
            wc.status = WifiConfiguration.Status.DISABLED;     
            wc.priority = 1;
            wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            wc.preSharedKey = "\"".concat(password).concat("\"");
            int res = wifi.addNetwork(wc);
    

    【讨论】:

      猜你喜欢
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      相关资源
      最近更新 更多