How to connect to wifi with a specific bssid android?

Boublond picture Boublond · Aug 20, 2015 · Viewed 8.1k times · Source

I am creating an Android app which allows to connect you to a WiFi with a specific BSSID. I have implemented the part which scan all the wifis and make the WiFi configuration in function of WiFi network security type. I have also implemented the connection to WiFi network with a specific BSSID.

But I have a problem: the connection to a specific BSSID works well for secure networks but doesn't work really for open network and I don't know why. Indeed this connection to an open network with a specific BSSID works on Samsung Galaxy S4 or more recent but doesn't work on Galaxy S2 and S3 ... It's really weird. On S2 and S3 the BSSID isn't take into account during connection.

This is the sample code that I use to create the configuration for a open network and try to connect on it with the specific BSSIDs:

    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + wifiItem.getSSID() + "\"";
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    WifiManager wifiManager = (WifiManager) parentActivity.getSystemService(Context.WIFI_SERVICE);

    wifiManager.addNetwork(conf);
    conf.BSSID = wifiItem.getBSSID();
    wifiManager.updateNetwork(conf);
    wifiManager.saveConfiguration();

    for (WifiConfiguration wifiConfiguration : wifiManager.getConfiguredNetworks()) {

        if (wifiConfiguration.SSID.equals("\"" + wifiItem.getSSID() + "\"")) {

            wifiManager.disconnect();
            wifiConfiguration.BSSID = wifiItem.getBSSID();
            wifiManager.updateNetwork(wifiConfiguration);
            wifiManager.enableNetwork(wifiConfiguration.networkId, true);
            wifiManager.reconnect();

        }

If someone could help me it would be so cool. I spent a lot of time on it and really don't understand this problem.

Answer

Kristy Welsh picture Kristy Welsh · Aug 20, 2015

You can try looking this code over, this is what we use to connect to networks:

    private void connectToNetwork(String password, ScanResult result, String capabilities) {
    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\"" + result.SSID + "\"";

    wc.hiddenSSID = true;
    wc.status = WifiConfiguration.Status.ENABLED;

    if (capabilities.contains("WPA2")) {
        wc.preSharedKey = "\"" + password + "\"";
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    }

    if (capabilities.contains("WPA")) {
        wc.preSharedKey = "\"" + password + "\"";
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    }

    if (capabilities.contains("WEP")) {
        wc.wepKeys[0] = password;
        wc.wepTxKeyIndex = 0;
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    }

    if (capabilities.contains("TKIP")) {
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    }

    if (capabilities.contains("CCMP")) {
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wc.allowedKeyManagement.set(WifiConfiguration.PairwiseCipher.CCMP);
    }

    if (!hasWifiSecurity(capabilities)) {
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.NONE);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    }

    // wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);

    // // wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    // wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

    int res = wifi.addNetwork(wc);
    Log.d("WifiPreference", "add Network returned " + res);
    boolean b = wifi.enableNetwork(res, true);
    Log.d("WifiPreference", "enableNetwork returned " + b);
    Log.d("", "Reassociate: " + wifi.reassociate());
    Log.d("", "ReConnect:   " + wifi.reconnect());
    wifi.saveConfiguration();

    WifiInfo connection = wifi.getConnectionInfo();
    if (connection != null) {
        if (connection.getSupplicantState().equals(SupplicantState.INACTIVE)) {
            wifi.removeNetwork(res);
            wifi.saveConfiguration();
            scanForWiFiNetworks();

            wifi.enableNetwork(wifi.getConnectionInfo().getNetworkId(), true);

        }
    }
    // scanForWiFiNetworks();

    // if (password.equalsIgnoreCase("")) {
    // setScanningEnabled(true);
    // }
    // Toast.makeText(con, "Connecting to network: " + connectionInfo, Toast.LENGTH_SHORT).show(getMessageComments(dialog.getTextID()));
}