【问题标题】:Network id is -1 for latest versions in androidandroid 中最新版本的网络 ID 为 -1
【发布时间】:2016-02-01 12:41:41
【问题描述】:

我创建了一个应用程序来连接到列表中选定的 Wifi。这在 4.1.2(网络 ID = 10)的 android 版本中运行良好,但如果我尝试在 4.4.2 和 4.4.4 版本上运行相同的应用程序,它会将网络 ID 值显示为 -1。

我的代码是:

private void connectToWifi2() {
    // TODO Auto-generated method stub

    // Main method. Write code here.
    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = String.format("\"%s\"", selWifi.SSID);
    conf.preSharedKey = String.format("\"%s\"", pwd.getText().toString());


    conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    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.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);

    conf.hiddenSSID = true;
    // remember id
    int netId = wifiManager.addNetwork(conf);
    Log.d("RMGWiFiConfig", "netId returned" + netId);
    if (netId >= 0) {
        Log.i("RMGWiFiConfig", "Connecting with " + selWifi.SSID);
        wifiManager.disconnect();
        Boolean statusInBoolean = wifiManager.enableNetwork(netId, true);
        Log.d("RMGWiFiConfig", "Enabled network returned "
                + statusInBoolean);
        Log.d("RMGWiFiConfig", "Reassociate : " + wifiManager.reassociate());
        Log.d("RMGWiFiConfig", "Reconnect " + wifiManager.reconnect());

    } else {
        Log.d("RMGWifiConfig", "Failed to add network configuration");
    }

}

我尝试参考以下链接但无法解决问题。或者我可能无法以以下链接中提供的正确方式适应解决方案。请让我知道我需要对我的代码进行的更改。

Can not connect another android device with wifi with android lollipop

Android WifiConfiguration shows -1 for ID. How can I fix it for SSID to be recognized?

Android 6.0 Cannot add WifiConfiguration if there is already another WifiConfiguration for that SSID

android wifiManager.addNetwork returns -1

WifiConfiguration enable network in Lollipop

Android 5.0 Lollipop and 4.4 KitKat ignores my WiFi network, enableNetwork() is useless

请帮忙!

谢谢 修罗提

【问题讨论】:

标签: android android-version wifi


【解决方案1】:

我找不到明显的关键原因,但我有类似的代码在 Android 6.x 上运行。我能找到的区别是:

  • wc.StatusField = WifiStatus.Disabled;(enableNetwork() 稍后会更新)
  • wc.PreSharedKey = "\"" + pw + "\"";
  • 最后:wm.SaveConfiguration();

如果我不得不猜测,我会说这是导致失败的第一个原因。

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多