-1

I am developing an Android application in which I need to connect my phone to my laptop hotspot programmatically using Java. The scenario is, when I click a button in the application, my device should disconnect from the connected wifi network and should automatically connect to my laptop hotspot. I have implemented the code to disconnect my phone from the connected wifi network, however, I need to know how should I connect it to the laptop hotspot specifically.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175

1 Answers1

1

Using this method you can connect to a specific WIFI network

 private WifiManager wifiManager;
 wifiManager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
 connectToWifi("networkSSID", "networkPassword");


 /**
 * Connect to the specified wifi network.
 *
 * @param networkSSID     - The wifi network SSID
 * @param networkPassword - the wifi password
 */
private void connectToWifi(final String networkSSID, final String networkPassword) {
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }

    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = String.format("\"%s\"", networkSSID);
    conf.preSharedKey = String.format("\"%s\"", networkPassword);

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

Don't forget to add permissions in your Manifest file

enter image description here

Refer this blog for more info : wifimanager example

Tejas Dhawale
  • 393
  • 1
  • 9