Android wifimanager reset all settings - android

I tried to create an application for wifi connection. After running some piece of code, i can't even connect my phone to the wifi. I have this piece of code
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", "XXX");
wifiConfig.preSharedKey = String.format("\"%s\"", "xxxxxxxxxxx");
WifiManager wifiManager = (WifiManager)fragmentActivity.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
The method addNetWork() always returns -1. I'd like to know what piece of code I have to write to reset all wifi settings to find my wifi back.

Related

Programmatically connecting to a specific wifi

I would like to connect to specific wifi programmatically in android. I referred lot of links. But still not yet achieve the goal. I am following below code to connect specific wifi.
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration configuration = new WifiConfiguration();
configuration.SSID = String.format("\"%s\"", "ssid");
configuration.preSharedKey = String.format("\"%s\"", "paasword");
configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
int netId = wifiManager.addNetwork(configuration);
wifiManager.disconnect();
wifiManager.enableNetwork(netId,true);
wifiManager.reconnect();
When I was try to run this code stuff.. wifi connection will be disconnected and again automatically connected previous wifi network.I am using redmi 6a mobile(version 9) for testing purpose.
Can any one guide me how to resolve this one.
Thanks in advance.
I believe you need to loop through all available networks, then connect to right one if it is available.
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}else{Log.e("TAG","Network Not Available")}
}

Connect android device to wifi programmatically

I need to connect my tablet to a wifi programmaticaly. I have tried a least 20 differents codes, nothing works.
I have all the permissions :
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
And here is the code :
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", "ssis");
wifiConfig.preSharedKey = String.format("\"%s\"", "password");
wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
The wifi protocol I use is WPA/WPA2
Does soemone has a working code?
Try this code this will work on all device except android 10.
Check this https://issuetracker.google.com/issues/128554616
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", networkSSID);
wifiConfig.preSharedKey = String.format("\"%s\"", networkPass);
wifiManager = (WifiManager)
getActivity().getApplicationContext().getSystemService(WIFI_SERVICE);
if(!wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(true);
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();

Android, Wifi reconnect not working

I have an Wifi router and I try to make connection like this:
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + wifiSSID + "\"";
wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfig.preSharedKey = "\"" + wifiPassword + "\"";
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
int netId = wifiManager.addNetwork(wifiConfig);
Log.d("WIFI_RECONNECT", netId + "");
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
boolean recon = wifiManager.reconnect();
Log.d("WIFI_RECONNECT", recon + "");
When I launch this code I receive this in my logs:
D/WIFI_RECONNECT: "My_WIFI_SSID" 1234567890
D/WIFI_RECONNECT: 67
D/WIFI_RECONNECT: true
However, my device shows that I have disconnected from all wifi routers that is accessible to me and my routers wifi network is shown in my available wifi list but not in connected mode.
Why this is happening?
Thats happened cause WifiManager.reconnect() returns true if the operation succeeded and not when connection is completed. You should catch Wi-Fi state with Broadcast receiver after reconnect()

How to carry the same wifi connection from one activity to another

I have connected some WiFi pragmatically in One activity and I need same connection in another activity. I don't want to disconnect wifi connections while switching from activity to another.
Here is the code to connect to wifi ,
WifiInfo connInfo = wifiManager.getConnectionInfo();
if (null != connInfo) {
//connect to previous network
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + prefSSID + "\"";
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
otvNetworkID = wifiManager.addNetwork(wifiConfig);
wifiManager.enableNetwork(otvNetworkID, true);
wifiManager.saveConfiguration();

Can't set "WifiConfiguration" when enabling wifi-hotspot using "setWifiApEnabled"

I'm trying to set my Android device to be an Access-Point using the code I've seen here before:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "MyAccessPoint";
Method method = wifi.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifi, netConfig, true);
now, I managed to turning it on but without the SSID which I set in WifiConfiguration.
This is driving me crazy.
Anyone?
Before Invoking the Method "setWifiApEnabled" you need to call "getWifiApConfiguration" to get the default WifiConfiguration
Then change the SSID and Password and then invoke "setWifiApConfiguration" with the modified WifiConfiguration and after that call "setWifiApEnabled"
Here is the Code.
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
getWifiConfig = wifi.getClass().getMethod("getWifiApConfiguration",null);
WifiConfiguration myConfig = (WifiConfiguration) getWifiConfig.invoke(wifi,null);
myConfig.SSID = "Hello World";
setWifiConfig = wifi.getClass().getMethod("setWifiApConfiguration",WifiConfiguration.class);
setWifiConfig.invoke(wifi,new Object[]{myConfig,true});
enableWifi = wifi.getClass().getMethod("setWifiEnabled",WifiConfiguration.class,boolean.class);
enableWifi.invoke(wifi,null,true);
See how I got this working at Android 2.3 wifi hotspot API.

Categories

Resources