failed to connect to specific WiFi in android programmatically

I'm using the following code to detect and connect to specific WiFi ssid when I press a button in android. Below is the code. Any help will be appreciated.

ssid :- "myHotspot" & password:- "12345678"

Button b1 = (Button) findViewById(R.id.button); <br> b1.setOnClickListener(new View.OnClickListener() { <br><br>@Override <br>public void onClick(View v) { wifiConfiguration.SSID = "\"myHotspot\""; wifiConfiguration.preSharedKey ="\"12345678\""; WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); int netId = wifiManager.updateNetwork(wifiConfiguration); if (wifiManager.isWifiEnabled()) { //---wifi is turned on--- //---disconnect it first--- wifiManager.disconnect(); } else { //---wifi is turned off--- //---turn on wifi--- wifiManager.setWifiEnabled(true); } wifiManager.enableNetwork(netId, true); wifiManager.reconnect(); } });

The main problem I'm getting is that my phone gets connected to the ssid and after 2-3 seconds it loses the connection and gets connected to my home Wifi router (which has internet connectivity)

Note:- The ssid I'm trying to connect is just a local hotspot without any internet connection. and if I try with "addNetwork(wifiConfiguration)" it creates multiple networks of the same name. so Now how do I resolve this ?!


I think the problem here is you try to enableNetwork immediately after the call to wifiManager.setWifiEnabled(true). Generally, switching on wifi will take 5-10 seconds depending on the device, until then any call to wifiManager.enableNetwork will be lost. Hence, your call to connect to the desired network is getting lost and as soon as the wifi is switched on, your device connects to the last network it remembers.

Try to create a loop where you keep checking if wifiManager.isWifiEnabled() == true and keep looping until it returns true (with Thread.sleep() obviously and doing this in an AsyncTask or separate Thread). Only after that try to call enableNetwork.


  • How to get unique id of WiFi in android?
  • How can I read or get the information of the beacon frame that sent by the Access point in WLAN, by
  • Access method and varaible in MainActivity from extern BroadcastReceiver
  • Android How to scan for hidden access points
  • How do I program android to look for a particular network?
  • Differences between WifiConfiguration status
  • Android Asynctask method does not display the data
  • iOS programatically setting up device in AP mode
  • Android Getting WiFi signal strength as it changes
  • Android WifiConfiguration shows -1 for ID. How can I fix it for SSID to be recognized?
  • In DynamoDB how do I append an element to a list field using Java
  • AWS CloudFormation: Passing Values between Nested Stacks
  • Get IMEI code or/and Mac Address of a Device
  • Get Mac address of client connected with esp8266
  • Get SSID's in range iOS 7
  • Word 2010 Templates/Addin/Document passing starting arguments
  • better way to communicate between ad hoc wifi windows mobile devices
  • Connect to strongest access point (access point with the strongest signal) with the same SSID
  • How to get the index of element in the List in c#
  • ASP.NET RegularExpressionValidator, validate on a non-match?
  • How to override jQuery promise callback
  • Generating anchors with PyYAML.dump()?
  • Replace Fragment with another on back button
  • Swift Initialization Rule Confusion
  • Change Checkbox value without raising event
  • LiveData is abstract android
  • CSS - Cannot get one spanned style to override another inherited style and align left
  • Authentication failed with Azure Active Directory in Windows Phone
  • How to get listview position?
  • Regarding starting the threads on a condition
  • Center align outputs in ipython notebook
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Zoom in and out of jPanel
  • GridView breaks while scrolling
  • NHibernate Validation Localization with S#arp Architecture
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • unknown Exception android
  • failed to connect to specific WiFi in android programmatically