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.
Asked
Active
Viewed 395 times
1 Answers
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
Refer this blog for more info : wifimanager example

Tejas Dhawale
- 393
- 1
- 9
-
It works perfectly now, although there were some bumps in the beginning... Thanks – abdul razik fakih Oct 03 '20 at 10:45
-
@abdulrazikfakih Glad, I could help. Please approve/upvote the answer if it satisfies the question! Thank you :) – Tejas Dhawale Oct 03 '20 at 11:38