Connect android device to wifi programmatically - android

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();

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")}
}

Why does getBSSID() always return "02:00:00:00:00:00"?

"02:00:00:00:00:00" does mean that the caller has insufficient permissions to access the BSSID. Why do I have insufficient permission? Whether I input a wrong or the right password it always returns "02:00:00:00:00:00". Even if I wait till the connection is established, it always returns the same value. I also have the permissions set in AndroidManifest.xml:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" android:required="true" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
And this Java Code:
WifiConfiguration wifiConfig = new WifiConfiguration();
String ssid = "AndroidAPC572";
wifiConfig.preSharedKey = String.format("\"%s\"", "password");
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int netId = wifiManager.addNetwork(wifiConfig);
boolean disconnected = wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
boolean reconnected = wifiManager.reconnect();
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
wifiInfo.getBSSID(); //always "02:00:00:00:00:00" / insufficient permission

Can I set ssid of WiFi?

I can use the following code to get the name of Wifi, I hope to select WiFi programatically, how can I do?
It seems that wifiInfo.ssid is val , and it can't be assigned!
I set the required permission as
<!-- in AndroidManifest.xml -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Then in the code
<!-- in Activity class -->
var wifiManager = mContext.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
var wifiInfo = wifiManager.connectionInfo
var name=wifiInfo.ssid
var isEnabled=wifiManager.isWifiEnabled
wifiInfo.ssid="MyNewWifi" //It cause error
BTW,
I have read the artical
How do I connect to a specific Wi-Fi network in Android programmatically?
It seems that I need to provide passsword in the above code when I reconnect the WIFI again.
In my mind, the password will be saved to configuration if I have connected to the wifi successfully, I hope that I needn't provide password in my code if I want to reconnect the WiFi again, how can I do?
You need to create a Wifi configuration like this.
String networkSSID = "testwifi";
String networkPass = "password";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.preSharedKey = "\""+ networkPass +"\"";
WifiManager wifiManager =
(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
This code should work for WPA security settings.
Reference:
Another similar question link

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()

Android wifimanager reset all settings

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.

Categories

Resources