Android — connect to wifi programmatically
When I have wifi enabled on my phone, it works as expected, but the problem is, when wifi is disabled. In this case the only result is enabling wifi adapter, but not connecting to the network. It seems like enabling takes to long so it won’t get connected. Another strange thing to me is that wifiManager.getConfiguredNetworks() returns null. Do you know how to fix that? Thank you
2 Answers 2
It seems like enabling takes to long so it won’t get connected.
Yes. This is because enabling of the network is done async, it happens in parallel, and doesn’t occur immediately. Here are two possible solutions to your problem:
1) This is the easiest solution, but not the best. Loop as described by another user checking for the scan results to come in. However, you should add a sleep of some sort between every cycle of the loop. I.E. you want to wait for 1ms, so as to not eat up all the CPU resources. I am not sure how to do this in Android off the top of my head. There is another problem with this method. If u are in the GUI thread, you will block all GUI events this way, as you wait for the connection to be established.
2) This is the proper solution. You can register for broadcast events after the network connection has been established. Then you will get an event when it finishes. From this event you can finish performing whatever operations are needed.
Sorry for the rushed answer. I am not an Android pro, so I can’t explain the details as to how to do this off the top of my head, but I thought I would at least point you in the right direction.
How to connect wifi in android
I’m new student for android app developing, Currently I did the Android Wifi connection code in order to make the connectivity.The app is showing the available connections but I cannot possible to connect in to specific wifi connections. Below is the one connectivity when i get from searching and i can see lot of these type of connections in my university premises. Ex: capabilities [WPA2-PSK CCMP][WPS][ESS],level:-37,freequency 2412 timestamp: 9103895476 could you please help me to overcome this problem and connect correctly to available connections. Also i have decide to implement Wifi ON/OFF button and didnt have clear idea for this implementation.. Below is my Java code
TextView mainText; WifiManager mainWifi; WifiReceiver receiverWifi; List wifiList; StringBuilder sb = new StringBuilder(); public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_wifi_connections); mainText = (TextView) findViewById(R.id.mainText); // Initiate wifi service manager mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); // Check for wifi is disabled if (mainWifi.isWifiEnabled() == false) < // If wifi disabled then enable it Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show(); mainWifi.setWifiEnabled(true); >// wifi scaned value broadcast receiver receiverWifi = new WifiReceiver(); // Register broadcast receiver // Broacast receiver will automatically call when number of wifi connections changed registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); mainWifi.startScan(); mainText.setText("Starting Scan. "); > public boolean onCreateOptionsMenu(Menu menu) < menu.add(0, 0, 0, "Refresh"); return super.onCreateOptionsMenu(menu); >public boolean onMenuItemSelected(int featureId, MenuItem item) < mainWifi.startScan(); mainText.setText("Starting Scan"); return super.onMenuItemSelected(featureId, item); >protected void onPause() < unregisterReceiver(receiverWifi); super.onPause(); >protected void onResume() < registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); super.onResume(); >// Broadcast receiver class called its receive method // when number of wifi connections changed class WifiReceiver extends BroadcastReceiver < // This method call when number of wifi connections changed public void onReceive(Context c, Intent intent) < sb = new StringBuilder(); wifiList = mainWifi.getScanResults(); sb.append("\n Number Of Wifi connections :"+wifiList.size()+"\n\n"); for(int i = 0; i < wifiList.size(); i++)< sb.append(new Integer(i+1).toString() + ". "); sb.append((wifiList.get(i)).toString()); sb.append("\n\n"); >mainText.setText(sb); > >
android turning on wifi programmatically
I am trying to turn add a wifi network programmatically and to connect to that network. My code works fine if the wi-fi is already turned on. If wi-fi is off, what i see wifimanager.addtonetwork() fails and when i see the wifi settings for the phone, i can see the status as scanning If i try to connect again it works fine. Please see code below. Please help
private int changeNetwork(NetworkSetting setting) < // If the SSID is empty, throw an error and return if (setting.getSsid() == null || setting.getSsid().length() == 0) < return doError(R.string.wifi_ssid_missing); >// If the network type is invalid if (setting.getNetworkType() == NetworkType.NETWORK_INVALID) < return doError(R.string.wifi_type_incorrect); >// If the password is empty, this is an unencrypted network if (setting.getPassword() == null || setting.getPassword().length() == 0 || setting.getNetworkType() == null || setting.getNetworkType() == NetworkType.NETWORK_NOPASS) < return changeNetworkUnEncrypted(setting); >if (setting.getNetworkType() == NetworkType.NETWORK_WPA) < return changeNetworkWPA(setting); >else < return changeNetworkWEP(setting); >> private int doError(int resource_string) < statusView.setText(resource_string); // Give up on the connection wifiManager.disconnect(); if (networkId >0) < wifiManager.removeNetwork(networkId); networkId = -1; >if (receiverRegistered) < unregisterReceiver(wifiReceiver); receiverRegistered = false; >return -1; > private WifiConfiguration changeNetworkCommon(NetworkSetting input) < statusView.setText(R.string.wifi_creating_network); Log.d(TAG, "Adding new configuration: \nSSID: " + input.getSsid() + "\nType: " + input.getNetworkType()); WifiConfiguration config = new WifiConfiguration(); config.allowedAuthAlgorithms.clear(); config.allowedGroupCiphers.clear(); config.allowedKeyManagement.clear(); config.allowedPairwiseCiphers.clear(); config.allowedProtocols.clear(); // Android API insists that an ascii SSID must be quoted to be correctly // handled. config.SSID = NetworkUtil.convertToQuotedString(input.getSsid()); config.hiddenSSID = true; return config; >private int requestNetworkChange(WifiConfiguration config) < statusView.setText(R.string.wifi_changing_network); return updateNetwork(config, false); >// Adding a WEP network private int changeNetworkWEP(NetworkSetting input) < WifiConfiguration config = changeNetworkCommon(input); String pass = input.getPassword(); if (NetworkUtil.isHexWepKey(pass)) < config.wepKeys[0] = pass; >else < config.wepKeys[0] = NetworkUtil.convertToQuotedString(pass); >config.allowedAuthAlgorithms .set(WifiConfiguration.AuthAlgorithm.SHARED); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0; return requestNetworkChange(config); > // Adding a WPA or WPA2 network private int changeNetworkWPA(NetworkSetting input) < WifiConfiguration config = changeNetworkCommon(input); String pass = input.getPassword(); // Hex passwords that are 64 bits long are not to be quoted. if (HEX_DIGITS_64.matcher(pass).matches()) < Log.d(TAG, "A 64 bit hex password entered."); config.preSharedKey = pass; >else < Log.d(TAG, "A normal password entered: I am quoting it."); config.preSharedKey = NetworkUtil.convertToQuotedString(pass); >config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); // For WPA config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA2 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); return requestNetworkChange(config); > // Adding an open, unsecured network private int changeNetworkUnEncrypted(NetworkSetting input) < Log.d(TAG, "Empty password prompting a simple account setting"); WifiConfiguration config = changeNetworkCommon(input); config.wepKeys[0] = ""; config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0; return requestNetworkChange(config); >/** * If the given ssid name exists in the settings, then change its password * to the one given here, and save * * @param ssid */ private WifiConfiguration findNetworkInExistingConfig(String ssid) < ListexistingConfigs = wifiManager .getConfiguredNetworks(); Log.i("Start comparing","Size "+existingConfigs.size() ); for (WifiConfiguration existingConfig : existingConfigs) < Log.i("Compare with SSID", ssid + existingConfig.SSID); if (existingConfig.SSID.equals(ssid)) < Log.i("Compare success with SSID", ssid + existingConfig.SSID); return existingConfig; >> return null; > @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); Intent intent = getIntent(); /* if (intent == null || !intent.getAction().equals(Intents.WifiConnect.ACTION)) < finish(); return; >*/ String ssid = intent.getStringExtra("ssid"); String password = intent.getStringExtra("password"); String networkType = intent.getStringExtra("type"); setContentView(R.layout.network); statusView = (TextView) findViewById(R.id.networkStatus); NetworkType networkT; if ("WPA".equals(networkType)) < networkT = NetworkType.NETWORK_WPA; >else if ("WEP".equals(networkType)) < networkT = NetworkType.NETWORK_WEP; >else if ("nopass".equals(networkType)) < networkT = NetworkType.NETWORK_NOPASS; >else < networkT = NetworkType.NETWORK_INVALID; >// This is not available before onCreate wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE); // Start WiFi, otherwise nothing will work wifiManager.setWifiEnabled(true); // So we know when the network changes wifiReceiver = new WifiReceiver(wifiManager, this, statusView, ssid); // The order matters! mWifiStateFilter = new IntentFilter( WifiManager.WIFI_STATE_CHANGED_ACTION); mWifiStateFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); mWifiStateFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); mWifiStateFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); registerReceiver(wifiReceiver, mWifiStateFilter); receiverRegistered = true; if (password == null) < password = ""; >Log.d(TAG, "Adding new configuration: \nSSID: " + ssid + "Type: " + networkT); NetworkSetting setting = new NetworkSetting(ssid, password, networkT); changeNetwork(setting); > @Override public void onPause() < super.onPause(); if (receiverRegistered) < unregisterReceiver(wifiReceiver); receiverRegistered = false; >> @Override public void onResume() < super.onResume(); if (wifiReceiver != null && mWifiStateFilter != null && !receiverRegistered) < registerReceiver(wifiReceiver, mWifiStateFilter); receiverRegistered = true; >> @Override protected void onDestroy() < if (wifiReceiver != null) < if (receiverRegistered) < unregisterReceiver(wifiReceiver); receiverRegistered = false; >wifiReceiver = null; > super.onDestroy(); > /** * Update the network: either create a new network or modify an existing * network * * @param config * the new network configuration * @param disableOthers * true if other networks must be disabled * @return network ID of the connected network. */ private int updateNetwork(WifiConfiguration config, boolean disableOthers) < WifiConfiguration found = findNetworkInExistingConfig(config.SSID); wifiManager.disconnect(); if (found == null) < Log.i("WIFI","SSID NOT FOUND"); statusView.setText(R.string.wifi_creating_network); >else < statusView.setText(R.string.wifi_modifying_network); Log.d(TAG, "Removing network " + found.networkId); wifiManager.removeNetwork(found.networkId); wifiManager.saveConfiguration(); >networkId = wifiManager.addNetwork(config); Log.d(TAG, "Inserted/Modified network " + networkId); if (networkId < 0) < wifiManager.setWifiEnabled(true); networkId = wifiManager.addNetwork(config); Log.d(TAG, "Again Inserted/Modified network " + networkId); return FAILURE_NO_NETWORK_ID; >// Try to disable the current network and start a new one. if (!wifiManager.enableNetwork(networkId, disableOthers)) < networkId = FAILURE_NO_NETWORK_ID; return FAILURE_NO_NETWORK_ID; >errorCount = 0; wifiManager.reassociate(); return networkId; >
package com.idg.project.utils; import java.util.List; import java.util.regex.Pattern; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.util.Log; /** * @author Vikram Aggarwal * @author Sean Owen */ public final class WifiConfigManager < private static final String TAG = WifiConfigManager.class.getSimpleName(); private static final Pattern HEX_DIGITS = Pattern.compile("[0-9A-Fa-f]+"); private WifiConfigManager() < >public static void configure(final WifiManager wifiManager, final String ssid, final String password, final String networkTypeString) < Runnable configureRunnable = new Runnable() < public void run() < // Start WiFi, otherwise nothing will work if (!wifiManager.isWifiEnabled()) < Log.i(TAG, "Enabling wi-fi. "); if (wifiManager.setWifiEnabled(true)) < Log.i(TAG, "Wi-fi enabled"); >else < Log.w(TAG, "Wi-fi could not be enabled!"); return; >// This happens very quickly, but need to wait for it to enable. A little busy wait? int count = 0; while (!wifiManager.isWifiEnabled()) < if (count >= 10) < Log.i(TAG, "Took too long to enable wi-fi, quitting"); return; >Log.i(TAG, "Still waiting for wi-fi to enable. "); try < Thread.sleep(1000L); >catch (InterruptedException ie) < // continue >count++; > > NetworkType networkType = NetworkType.forIntentValue(networkTypeString); if (networkType == NetworkType.NO_PASSWORD) < changeNetworkUnEncrypted(wifiManager, ssid); >else < if (password == null || password.length() == 0) < throw new IllegalArgumentException(); >if (networkType == NetworkType.WEP) < changeNetworkWEP(wifiManager, ssid, password); >else if (networkType == NetworkType.WPA) < changeNetworkWPA(wifiManager, ssid, password); >> > >; new Thread(configureRunnable).start(); > /** * Update the network: either create a new network or modify an existing network * @param config the new network configuration * @return network ID of the connected network. */ private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) < Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID); if (foundNetworkID != null) < Log.i(TAG, "Removing old configuration for network " + config.SSID); wifiManager.removeNetwork(foundNetworkID); wifiManager.saveConfiguration(); >int networkId = wifiManager.addNetwork(config); if (networkId >= 0) < // Try to disable the current network and start a new one. if (wifiManager.enableNetwork(networkId, true)) < Log.i(TAG, "Associating to network " + config.SSID); wifiManager.saveConfiguration(); >else < Log.w(TAG, "Failed to enable network " + config.SSID); >> else < Log.w(TAG, "Unable to add network " + config.SSID); >> private static WifiConfiguration changeNetworkCommon(String ssid) < WifiConfiguration config = new WifiConfiguration(); config.allowedAuthAlgorithms.clear(); config.allowedGroupCiphers.clear(); config.allowedKeyManagement.clear(); config.allowedPairwiseCiphers.clear(); config.allowedProtocols.clear(); // Android API insists that an ascii SSID must be quoted to be correctly handled. config.SSID = quoteNonHex(ssid); return config; >// Adding a WEP network private static void changeNetworkWEP(WifiManager wifiManager, String ssid, String password) < WifiConfiguration config = changeNetworkCommon(ssid); config.wepKeys[0] = quoteNonHex(password, 10, 26, 58); config.wepTxKeyIndex = 0; config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); updateNetwork(wifiManager, config); >// Adding a WPA or WPA2 network private static void changeNetworkWPA(WifiManager wifiManager, String ssid, String password) < WifiConfiguration config = changeNetworkCommon(ssid); // Hex passwords that are 64 bits long are not to be quoted. config.preSharedKey = quoteNonHex(password, 64); config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); updateNetwork(wifiManager, config); >// Adding an open, unsecured network private static void changeNetworkUnEncrypted(WifiManager wifiManager, String ssid) < WifiConfiguration config = changeNetworkCommon(ssid); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); updateNetwork(wifiManager, config); >private static Integer findNetworkInExistingConfig(WifiManager wifiManager, String ssid) < ListexistingConfigs = wifiManager.getConfiguredNetworks(); for (WifiConfiguration existingConfig : existingConfigs) < if (existingConfig.SSID.equals(ssid)) < return existingConfig.networkId; >> return null; > private static String quoteNonHex(String value, int. allowedLengths) < return isHexOfLength(value, allowedLengths) ? value : convertToQuotedString(value); >/** * Encloses the incoming string inside double quotes, if it isn't already quoted. * @param string the input string * @return a quoted string, of the form "input". If the input string is null, it returns null * as well. */ private static String convertToQuotedString(String string) < if (string == null || string.length() == 0) < return null; >// If already quoted, return as-is if (string.charAt(0) == '"' && string.charAt(string.length() - 1) == '"') < return string; >return '\"' + string + '\"'; > /** * @param value input to check * @param allowedLengths allowed lengths, if any * @return true if value is a non-null, non-empty string of hex digits, and if allowed lengths are given, has * an allowed length */ private static boolean isHexOfLength(CharSequence value, int. allowedLengths) < if (value == null || !HEX_DIGITS.matcher(value).matches()) < return false; >if (allowedLengths.length == 0) < return true; >for (int length : allowedLengths) < if (value.length() == length) < return true; >> return false; > > package com.idg.project.utils; enum NetworkType < WEP, WPA, NO_PASSWORD; static NetworkType forIntentValue(String networkTypeString) < if (networkTypeString == null) < return NO_PASSWORD; >if ("WPA".equals(networkTypeString)) < return WPA; >if ("WEP".equals(networkTypeString)) < return WEP; >if ("nopass".equals(networkTypeString)) < return NO_PASSWORD; >throw new IllegalArgumentException(networkTypeString); > >
How to enable/disable WiFi from an application?
You also need to request the permission in your AndroidManifest.xml :
@Codii, I know this is old, but I am trying to do this within a dialogfragment. However, it says «Cannot resolve method ‘getSystemService(java.lang.String)’ I am not sure on what I need to do. And I have those permissions
To enable/disable WiFi in your application you need to use WiFiManager class. Create an Object of WiFiManager class to get the services of WiFi.
WifiManager wifi; wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE); wifi.setWifiEnabled(false);//Turn off Wifi wifi.setWifiEnabled(true);//Turn on Wifi
And you have to put the following permissions in AndroidManifest.xml
To get the whole sample code of enable/disable Wifi in android with UI visit this website
when i do it in my project, but when these code is executed, the system will give a prompt says the app is trying to use wlan whether allows it. and it appears every time! can i enable wlan without the prompt window .
Intent gpsOptionsIntent = new Intent( android.provider.Settings.ACTION_WIFI_SETTINGS); startActivityForResult(gpsOptionsIntent,0);
To enable/disable wifi from an app in Android Q (Android 10) use Settings Panel:
val panelIntent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY) startActivityForResult(panelIntent, 0)
On previous versions of Android this should work (appropriate permissions should be added to AndroidManifest file, see answers above):
(context?.getSystemService(Context.WIFI_SERVICE) as? WifiManager)?.apply < isWifiEnabled = true /*or false*/ >
Resulting code might look something like this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) < val panelIntent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY) startActivityForResult(panelIntent, 0) >else < (context?.getSystemService(Context.WIFI_SERVICE) as? WifiManager)?.apply < isWifiEnabled = true /*or false*/ >>
Where context is a reference to android.content.Context object.