Is possible to programmatically connect to Wi-Fi network using Xamarin? - android

Using Xamarin.iOS and Xamarin.Android, it is possible to check if the Wi-Fi is enabled?
And, if is disabled, it's possible to enable it?
Once it's enabled, how to search and connect to a network?

Enable/Disable Wifi:
WifiManager wifiManager = (WifiManager)GetSystemService(WifiService);
if (!wifiManager.IsWifiEnabled)
wifiManager.SetWifiEnabled(true);
else
wifiManager.SetWifiEnabled(false);
Permission to change Wifi state:
android.permission.CHANGE_WIFI_STATE
Add a network:
var networkSSID = "Stack";
var networkPass = "Overflow";
var config = new WifiConfiguration();
config.Ssid = '"' + networkSSID + '"';
// For WPA/WPA2, WEP is different (still using WEP? shame on you ;-)
config.PreSharedKey = '"' + networkPass + '"';
wifiManager.AddNetwork(config);
Connect to a network:
IList<WifiConfiguration> myWifi = wifiManager.ConfiguredNetworks;
wifiManager.Disconnect();
wifiManager.EnableNetwork(myWifi.FindFirst(x => x.Ssid.Contains(networkSSID)), true);
wifiManager.Reconnect();

Related

how do i add network in wificonfig after API level26

i am facing a problem with wifi if the ap has same ssid and password i am getting BSSID as null or any from getconfiguredNetwork() method. so, i want to add network manually into wifi configuration but this add,remove and update will not work from api levl 26. is there any other alternative way to solve this solution. Reference Link.
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.BSSID = Bssid;
conf.preSharedKey = "\"" + networkPasskey + "\"";
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.BSSID != null && i.BSSID.equals(Bssid)) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reassociate();
Log.d("changing network", "connecting the right network");
break;
}
}
You will require location permissions, starting with Android 8.1 and 9 Google changed how you can access WiFi info.
https://developer.android.com/about/versions/pie/android-9.0-changes-all#restricted_access_to_wi-fi_location_and_connection_information
You will need ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions as well as rely on the user having their location service enabled. This cannot be done any other way because of privacy issues relating to WiFi.

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 change wifi network not working

I am trying to connect to a specific wifi network, as described here. However, while sometimes it works, most of the times it doesn't. I do not really understand why if I run the app, let's say 10 times, the device connects to my network 2 times, and the other 8 it doesn't.
In the following is the code I use in a Fragment.
WifiConfiguration wificonfiguration = new WifiConfiguration();
wificonfiguration.SSID = "\"" + networkSSID + "\"";
wificonfiguration.priority = 40;
wificonfiguration.status = WifiConfiguration.Status.ENABLED;
// WPA management
wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wificonfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wificonfiguration.preSharedKey = "\"" + networkPass + "\"";
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
int networkId = wifiManager.addNetwork(wificonfiguration);
if (networkId > -1) {
boolean status = wifiManager.enableNetwork(networkId, true);
}
The network configuration is actually added to the manager and status value is true. I have run it on several devices running Lollipop. Also, the network I want to connect to does not provide internet access, so I also thought that may be a problem for what stated in the note here. Can anybody tell me if the code is ok?
Solved
The problem was solved by adding wifiManager.disconnect(); before adding the new network configuration.
You could disable all the network via below described method before adding the network. This would disable all the configured networks to auto-connect.
for (WifiConfiguration wifiConfiguration_ : wifiManager.getConfiguredNetworks()) {
wifiConfiguration_.status = WifiConfiguration.Status.DISABLED;
int returnNetworkId_ = wifiManager.updateNetwork(wifiConfiguration_);
}

How to check if password for WiFi network is correct in Android?

In my application I ask user to select WiFi network and to enter password (if needed). How can I check if password is correct?
Here is my code for connecting to WPA networks
conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
networkPass = input.getText().toString();
conf.preSharedKey = "\""+ networkPass +"\"";
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
if (list.size()>0)
{
for (WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
}
two option:
1) Use BroadcastReceiver like this.You receive an intent after your success connected. But you will not get one if the password is incorrect. So my suggestion is :
2) check it out later(like after 2s) use the Handler. Because associating and connecting to a WiFi network takes time and the actual task of connecting or associating with the network is done asynchronously in the background. So just check it out later( 1s is too short and 2 second is work for me. You might try other number).

Categories

Resources