在 Android 中以编程方式连接到 Wifi AP

Connecting to Wifi AP programmatically in Android

我实际上正在编写一些程序,不允许用户访问 android 本机 wifi 连接页面,所以我在我的 Android 应用程序中编写它。它扫描可用的 Wifi 网络,将其显示在 ListView 中,然后单击要连接的网络。

实际上除了我的连接函数外一切正常,这让我的应用程序崩溃了,所以如果你有任何想法......这是代码(这是在用户进入网络通行证后调用的函数)和堆栈跟踪:

 private void connectToNetwork(String networkSSID, String networkPass, String networkCapabilities) {

        WifiConfiguration wifiConfig = new WifiConfiguration();
        wifiConfig.SSID =networkSSID;

        if (networkCapabilities.toUpperCase().contains("WEP")) { // WEP Network.
            Toast.makeText(this, "WEP Network", Toast.LENGTH_SHORT).show();

            wifiConfig.wepKeys[0] = networkPass;
            wifiConfig.wepTxKeyIndex = 0;
            wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        } else if (networkCapabilities.toUpperCase().contains("WPA")) { // WPA Network
            Toast.makeText(this, "WPA Network", Toast.LENGTH_SHORT).show();
            wifiConfig.preSharedKey = networkPass;
        } else { // OPEN Network.
            Toast.makeText(this, "OPEN Network", Toast.LENGTH_SHORT).show();
            wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        }

        @SuppressLint("MissingPermission")
        List<WifiConfiguration> list = this.wifiManager.getConfiguredNetworks();
        for (WifiConfiguration config : list) {
            if (config.SSID != null && config.SSID.equals(networkSSID)) {
                this.wifiManager.disconnect();
                this.wifiManager.enableNetwork(config.networkId, true);
                this.wifiManager.reconnect();
                break;
            }
        }

    }

堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.net.wifi.WifiManager.getConfiguredNetworks()' on a null object reference
        at solutions.#####.############.WifiActivity.connectToNetwork(WifiActivity.java:148)

在@LevM 的帮助下,现在可以使用的完整代码。和一些自我研究。我 post 它是因为我没有找到太多解决方案来连接到 Android 互联网上的特定 Wifi 网络(SO,Youtube 或其他)所以如果它可以帮助至少一个开发者几天,我会要开心 ! :D(注意:我是一名学生程序员,我并不是说这个解决方案是最好的,但它至少是功能性的)

我的 wifiManager 没有被初始化,这是一个问题。然后我删除了 wifiConfiguration 列表循环,我意识到我不需要它。 最后,我在 SSID 和密码上添加了字符串格式,因为它们需要位于反斜线(“\”)内。

private boolean connectToNetwork(String networkSSID, String networkPass, String networkCapabilities) {
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiConfiguration wifiConfig = new WifiConfiguration();
        wifiConfig.SSID = String.format("\"%s\"", networkSSID);

        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }

        if (networkCapabilities.toUpperCase().contains("WEP")) { // WEP Network.
            Toast.makeText(this, "WEP Network", Toast.LENGTH_SHORT).show();

            wifiConfig.wepKeys[0] = String.format("\"%s\"", networkPass);
            ;
            wifiConfig.wepTxKeyIndex = 0;
            wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        } else if (networkCapabilities.toUpperCase().contains("WPA")) { // WPA Network
            Toast.makeText(this, "WPA Network", Toast.LENGTH_SHORT).show();
            wifiConfig.preSharedKey = String.format("\"%s\"", networkPass);
            ;
        } else { // OPEN Network.
            Toast.makeText(this, "OPEN Network", Toast.LENGTH_SHORT).show();
            wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        }


        int netId = wifiManager.addNetwork(wifiConfig);//  
        wifiManager.disconnect();
        wifiManager.enableNetwork(netId, true);
        return wifiManager.reconnect();

    }