Simple Android Wifi Connection

时间:2016-07-11 21:17:31

标签: android android-wifi

My code was returning Null Bss. Anyone knows how to implement it to work? Just want to connect to an Wifi NetWork from my home. Thanks.

public class Scan extends AppCompatActivity {

public String ssid;
public String password;
public String security;


public EditText nome, senha, tipo;
public Button connectar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    nome = (EditText) findViewById(R.id.etSSID);
    senha = (EditText) findViewById(R.id.etPASS);
    tipo = (EditText) findViewById(R.id.etTIPO);




}



public void connectar(View view){
    //Toast.makeText(Scan.this, "Conectado", Toast.LENGTH_SHORT).show();
    String ssidC = nome.getText().toString();
    String senhaC = senha.getText().toString();
    String tipoC = tipo.getText().toString();

    connect(ssidC, senhaC, tipoC);
}



public void connect(String ssid, String password, String security){

    WifiConfiguration wfc = new WifiConfiguration();

    wfc.SSID = "\"".concat(ssid).concat("\"");
    wfc.status = WifiConfiguration.Status.DISABLED;
    wfc.priority = 40;


    if(security.equals("WPA2")){
        wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
        wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

        wfc.preSharedKey = "\"".concat(password).concat("\"");

    }

    WifiManager wfMgr = (WifiManager)getSystemService(Context.WIFI_SERVICE);
    int networkId = wfMgr.addNetwork(wfc);
    if (networkId != -1) {
        wfMgr.enableNetwork(networkId, true);
    }

}

}

I am passing the SSID, PASSWORD and WPA2 on my EditText. The logcat was saying BSS was null.

0 个答案:

没有答案