Hi I know this question asked many time but non of theme help me to dispel my problem.
i want to create hotspot in a device and connect to it from other device and sending data over wifi.
Problem is first time i can connect to my hotspot and transfer data over that but in secound time when hotspot disabled and i create it again i cannot connect to that network until i restart my client phone.
here is the code for connecting to hotspot
addWifiConfig(HotspotName, Hotspotpass, "WPA", "");
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + HotspotName + "\"")) {
try {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
boolean res=wifiManager.reconnect();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
addWifiConfig methode
public static void addWifiConfig(String ssid,String password,String securityParam,String securityDetailParam) {
if (ssid == null) {
throw new IllegalArgumentException(
"Required parameters can not be NULL #");
}
String wifiName = ssid;
WifiConfiguration conf = new WifiConfiguration();
if (Build.VERSION.SDK_INT >= 21) {
conf.SSID = wifiName;
} else {
conf.SSID = "\"" + wifiName + "\"";
}
String security = securityParam;
if (security.equalsIgnoreCase("WEP")) {
conf.wepKeys[0] = password;
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (security
.equalsIgnoreCase("NONE")) {
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
} else if ("WPA"
.equalsIgnoreCase(security)
|| "WPA2"
.equalsIgnoreCase(security)
|| "WPA/WPA2 PSK"
.equalsIgnoreCase(security)) {
// appropriate ciper is need to set according to security type used,
// ifcase of not added it will not be able to connect
conf.preSharedKey = "\""
+ password + "\"";
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.status = WifiConfiguration.Status.ENABLED;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
}
String securityDetails = securityDetailParam;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int newNetworkId = wifiManager.addNetwork(conf);
wifiManager.saveConfiguration();
wifiManager.setWifiEnabled(true);
}
and here is error i got in secound time try to connect
java.net.ConnectException: failed to connect to /192.168.43.80 (port 8080):
connect failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.connect(IoBridge.java:114)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.Socket.startupSocket(Socket.java:566)
at java.net.Socket.tryAllAddresses(Socket.java:128)
at java.net.Socket.<init>(Socket.java:178)
at java.net.Socket.<init>(Socket.java:150)
at drawerfragments.SettingsFragment$MyClientTask.doInBackground(SettingsFragment.java:477)
at drawerfragments.SettingsFragment$MyClientTask.doInBackground(SettingsFragment.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
at libcore.io.Posix.connect(Native Method)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
... 14 more