How connect Android to no SSID public and no Passw

2019-09-02 10:22发布

问题:

i can't connect Android on SSID established, i try WifiConfiguration but unsucessfull, my ssid is open (without password).

In Android UI i can connect, but on code i can't.

Anybody help me?

Tankyou.

EDIT:

my code:

String networkSSID = "DL63.0.0.0.1";
        String networkPass = "";

        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";
//        conf.wepKeys[0] = "\"" + networkPass + "\"";
//        conf.wepTxKeyIndex = 0;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

        WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        wifiManager.addNetwork(conf);

        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration i : list) {
            if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                wifiManager.disconnect();
                wifiManager.enableNetwork(i.networkId, true);
                wifiManager.reconnect();

                Toast.makeText(this, "SSID: "+wifiManager.getConnectionInfo().getSSID(), Toast.LENGTH_SHORT).show();
                break;
            }
        }

        status.setText("SSID: "+wifiManager.getConnectionInfo().getSSID());

回答1:

Probably because wifiManager.reconnect(); starts an asynchronous process to connect. When your code reaches the last line, there is yet no connection (that takes time, may even be a minute or so), so null is the correct answer at that moment. You will need a listener to react on the connect event. See that question for details: https://stackoverflow.com/a/5890553/1503237