Help with Wifi Manager

1

I need to read a value of 2 edittext being SDDI and Password and connect to that access point with this information

example:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect2);

        cone = (Button) findViewById(conec);
        sddi = (EditText) findViewById(R.id.editText);
        senha = (EditText) findViewById(R.id.editText2);


        cone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sddi.getText().toString().equalsIgnoreCase("") && senha.getText().toString().equals(" ")) {
                    Toast.makeText(getApplicationContext(), "Emtre com o Sddi e senha", Toast.LENGTH_LONG).show();
                } else {

                    mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

                    // Check for wifi is disabled
                    if (mainWifi.isWifiEnabled() == false) {
                        // If wifi disabled then enable it
                        Toast.makeText(getApplicationContext(), "Wifi desativo, ativando-o",
                                Toast.LENGTH_LONG).show();

                        mainWifi.setWifiEnabled(true);

                        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                        WifiConfiguration wc = new WifiConfiguration();
                        wc.SSID = String.valueOf(sddi);
                        wc.preSharedKey  = String.valueOf(senha);
                        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);

                    }


                }


            }
        });


    }
}
    
asked by anonymous 25.04.2016 / 22:19

1 answer

0

Try this:

wc.preSharedKey  =  senha.getText().toString();

Passwords are EditText and what you want is the text value ( getText ).

In this way: String.valueOf(senha) you are taking the object reference.

    
26.04.2016 / 15:32