Help with Wifi Manager

Asked

Viewed 113 times

1

I need to read a value of 2 edittext being SDDI and Password and connect this 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);

                    }


                }


            }
        });


    }
}
  • 1

    I don’t understand the question.... Have you had any problems? Has there been an exception? Where do you need help?

  • I already got thank you.

1 answer

0


Try it this way:

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

The passwords is a EditText and what you want is the value of the text ( getText ).

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

  • sorry the delay I did as you say and still did not work.

  • Is there an error? Can you tell why it didn’t work? Thanks!

  • I did, but I used another code if you want I can post here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.