1

Nowadays, I'm working on an application which must connect to a specific SSID and create a socket with AP. I want to get the event when the connection is established via receiver. I'm using this code to connect to SSID:

public boolean connectToSSID(Context context, final String ssid, final String pass) {
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + ssid + "\""; // Please note the quotes.
        // String should
        // contain ssid in quotes

        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        List<ScanResult> networkList = wifiManager.getScanResults();

        setText("Check WiFi devices");
        if (networkList != null) {
            for (ScanResult network : networkList) {
                // check if current connected SSID
                if (network.SSID.equals(ssid)) {
                    // get capabilities of current connection
                    String Capabilities = network.capabilities;
                    // Log.d (TAG, network.SSID + " capabilities : " +
                    // Capabilities);

                    if (Capabilities.contains("WPA2")) {
                        conf.preSharedKey = "\"" + pass + "\"";
                    } else if (Capabilities.contains("WPA")) {
                        conf.preSharedKey = "\"" + pass + "\"";
                    } else if (Capabilities.contains("WEP")) {
                        conf.wepKeys[0] = "\"" + pass + "\"";
                        conf.wepTxKeyIndex = 0;
                        conf.allowedKeyManagement
                                .set(WifiConfiguration.KeyMgmt.NONE);
                        conf.allowedGroupCiphers
                                .set(WifiConfiguration.GroupCipher.WEP40);
                    }

                    wifiManager.addNetwork(conf);
                    List<WifiConfiguration> list = wifiManager
                            .getConfiguredNetworks();
                    for (WifiConfiguration i : list) {
                        if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
                            wifiManager.disconnect();
                            wifiManager.enableNetwork(i.networkId, true);
                            wifiManager.reconnect();
                            /*if(isWifiConnected(context)){
                                setText("Connected to SSID");
                                return true;
                            }else{
                                setText("Unable to connect");
                                return false;
                            }*/
                            return true;
                        }
                    }
                    break;
                }
            }
        }
        return false;
    }

I've already read these questions but I couldn't find my answer.

How do I connect to a specific Wi-Fi network in Android programmatically?

How do I see if Wi-Fi is connected on Android?

How to detect when WIFI Connection has been established in Android?

How to auto connect a WiFi with specified SSID?

The question is ho can I get the connectivity event via receiver? Thanks in Advance

Mohsen Hatami
  • 257
  • 2
  • 13

0 Answers0