0

I want to connect on Wifi programmatically. I did some test on my old device (bought 4 years ago)and it worked perfectly. However when I did some test on my emulator (android 8.1.0, API 27), it did not not work. Do you have an explanation? (Actually I can see my wifi saved on the saved network of the settings but the device is not connected to the wifi) Here is my code (that I basically copied from an answer in a stack overflow question: How do I connect to a specific Wi-Fi network in Android programmatically?):

findViewById(R.id.connect_to_wifi).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Thread connectionThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    wifiManager.setWifiEnabled(true);
                    String ssid = ((TextView)findViewById(R.id.wifi_name)).getText().toString();
                    String key = ((TextView)findViewById(R.id.wifi_password)).getText().toString();
                    WifiConfiguration conf = new WifiConfiguration();
                    conf.SSID = String.format("\"%s\"", ssid);
                    conf.status = WifiConfiguration.Status.ENABLED;
                    conf.priority = 40;
                    //wifiConfig.preSharedKey = String.format("\"%s\"", key);
                    if (((EditText)findViewById(R.id.encryption)).getText().toString().equals("WEP")) {
                        Log.v("rht", "Configuring WEP");
                        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                        conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                        conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                        conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                        conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);

                        if (key.matches("^[0-9a-fA-F]+$")) {
                            conf.wepKeys[0] = key;
                        } else {
                            conf.wepKeys[0] = "\"".concat(key).concat("\"");
                        }

                        conf.wepTxKeyIndex = 0;

                    } else if (((EditText)findViewById(R.id.encryption)).getText().toString().contains("WPA")) {
                        Log.v("rht", "Configuring WPA");

                        conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                        conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

                        conf.preSharedKey = "\"" + key + "\"";

                    } else {
                        Log.v("rht", "Configuring OPEN network");
                        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                        conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                        conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                        conf.allowedAuthAlgorithms.clear();
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                    }

                    int netId = wifiManager.addNetwork(conf);
                    wifiManager.disconnect();
                    wifiManager.enableNetwork(netId, true);
                    wifiManager.reconnect();
                }
            });
            connectionThread.start();
        }
    });

Here is the log:

09-04 09:16:34.348 7410-8091/amiin.bazouk.application.com.localisationdemo V/rht: Configuring WPA
09-04 09:16:34.352 1725-2739/system_process I/WifiService: addOrUpdateNetwork uid=10079
09-04 09:16:34.352 1725-2739/system_process I/addOrUpdateNetwork:  uid = 10079 SSID "partner" nid=-1
09-04 09:16:34.357 1725-2497/system_process I/WifiService: getConfiguredNetworks uid=10014
09-04 09:16:34.377 1725-2041/system_process D/WifiConfigStore: Writing to stores completed in 21 ms.
09-04 09:16:34.381 1725-2497/system_process I/WifiService: disconnect uid=10079
09-04 09:16:34.383 7831-7831/? E/wpa_supplicant: nl80211: Failed to open /proc/sys/net/ipv4/conf/wlan0/drop_unicast_in_l2_multicast: No such file or directory
    nl80211: Failed to set IPv4 unicast in multicast filter
09-04 09:16:34.384 1725-2497/system_process I/WifiService: enableNetwork uid=10079 disableOthers=true
09-04 09:16:34.391 1397-3075/? W/audio_hw_generic: Hardware backing HAL too slow, could only write 0 of 720 frames
09-04 09:16:34.393 1725-2041/system_process D/WifiStateMachine: connectToUserSelectNetwork netId 8, uid 10079, forceReconnect = false
09-04 09:16:34.396 1725-2739/system_process I/WifiService: getConfiguredNetworks uid=10014
09-04 09:16:34.405 1397-3075/? W/audio_hw_generic: Hardware backing HAL too slow, could only write 0 of 720 frames
09-04 09:16:34.421 1397-3075/? W/audio_hw_generic: Hardware backing HAL too slow, could only write 0 of 720 frames
09-04 09:16:34.439 1725-2041/system_process D/WifiConfigStore: Writing to stores completed in 27 ms.
09-04 09:16:34.439 1725-2041/system_process E/WifiConfigManager: UID 10079 does not have permission to update configuration "partner"WPA_PSK
09-04 09:16:34.439 1725-2041/system_process I/WifiStateMachine: connectToUserSelectNetwork Allowing uid 10079 with insufficient permissions to connect=8
09-04 09:16:34.440 1725-2041/system_process D/WifiStateMachine: CMD_START_CONNECT sup state DisconnectedState my state DisconnectedState nid=8 roam=false
09-04 09:16:34.443 1725-2497/system_process I/WifiService: reconnect uid=10079
09-04 09:16:34.446 1562-1562/? E/wificond: Received error messsage: Operation not supported on transport endpoint
    NL80211_CMD_ABORT_SCAN failed
09-04 09:16:34.446 1562-1562/? W/wificond: Abort scan failed
09-04 09:16:34.447 1725-2041/system_process D/SupplicantStaIfaceHal: connectToNetwork "partner"WPA_PSK
    Network is already saved, will not trigger remove and add operation.
09-04 09:16:34.699 1725-2497/system_process D/WificondControl: Scan result ready event
09-04 09:16:34.715 1725-2041/system_process W/WifiConfigManager: Looking up network with invalid networkId -1
Bazouk55555
  • 557
  • 6
  • 24
  • Can you post the logs ? – Maxouille Sep 04 '18 at 09:11
  • you should try to test your code on a real device. As you can see in your logs emulator is unable to detect ipv4 settings and also logs are telling that operation is not supported. So your code is alright but you need to test on a real device to check if it really works or not. – Umair Sep 04 '18 at 09:22
  • @Maxouille I have posted the log. – Bazouk55555 Sep 04 '18 at 09:24
  • @Umair I do not have a recent device unfortunately...that is why I tried on an emulator...but my friend who is far away from me told me it does not work on his new device – Bazouk55555 Sep 04 '18 at 09:24

2 Answers2

0
  • set conf.BSSID
  • add double quotes(") before and after conf.SSID and conf.preSharedKey if missing.
Asim Habib
  • 381
  • 1
  • 4
  • 12
0

Android 8.1+ will not connect dirrectly to wifi first add permission at run time for COURSE_LOCATION then it will connect

Anek Pal
  • 1
  • 3