如何通过wifi配置连接到wifi

时间:2018-11-09 13:30:34

标签: java android

我试图通过连接到特定的wifi来了解android中的wifi部分,并且我确实将我的设备连接到了我配置成功的wifi,但是问题是它始终会给出“连接成功”并移至下一个活动,无论其是否连接到我的wifi,我都希望在连接时他可以访问下一个活动,否则他无法

MainClass.java

public String networkSSID = "helloworld";
public String networkPass = "it123456";
ImageButton ConnectButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wifi);
    ConnectButton = (ImageButton) findViewById(R.id.enter);
    ConnectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            networkSSID = "helloworld";
            networkPass = "it123456";

            WifiConfiguration conf = new WifiConfiguration();
            conf.SSID = "\"" + networkSSID + "\"";   // Please note the quotes. String should contain ssid in quotes

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

            /* for wpa*/
            conf.preSharedKey = "\"" + networkPass + "\"";

            /* for open network*/
            //conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

            Context context = getApplicationContext();
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

            wifiManager.setWifiEnabled(true);

            wifiManager.addNetwork(conf);

            List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
            for (WifiConfiguration i : list)
                if (i.SSID != null && i.SSID.equals("helloworld" + networkSSID + "it123456")) {
                    wifiManager.disconnect();
                    wifiManager.enableNetwork(i.networkId, true);
                    wifiManager.reconnect();
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    while (wifiInfo.getSSID() == null) {
                        Log.i("WifiStatus", "Here I am");
                        try {
                            Thread.sleep(Time.SECOND);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        wifiInfo = wifiManager.getConnectionInfo();
                    }
                    System.out.println("Connection established");
                    break;
                }
            if (networkSSID == "helloworld" && networkPass == "it123456") {
                Toast.makeText(context, "Connection success", 400).show();
            } else {
                Toast.makeText(context, "Connection fail", 400).show();
            }

            startActivity(new Intent(wifi.this, MainActivity.class));
        }
    });
}

0 个答案:

没有答案