I'm going to create xposed module that will add one option to the window which appears when user clicks/holds one of WiFi networks. The dialog box where are located options like "connect", "modify", "cancel".
Where do I find these methods?
public void connectTowifi()
{
String ssid = ""// ssid of clicked network
String pass = "" // pass of clicked network
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" ssid +"\"";
wifiConfig.preSharedKey = "\"" + OfflineUtils.generatePassword(ssid) + "\"";
wifiManager.addNetwork(wifiConfig);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration wifiConfiguration : list )
{
if(wifiConfiguration!=null && wifiConfiguration.SSID != null && wifiConfiguration.SSID.equals(wifiConfig.SSID))
{
wifiManager.disconnect();
wifiManager.enableNetwork(wifiConfiguration.networkId, true);
wifiManager.reconnect();
}
}
}
Similarly for diabling network use
http://developer.android.com/reference/android/net/wifi/WifiManager.html#disableNetwork(int)
for forgetiing network
http://developer.android.com/reference/android/net/wifi/WifiManager.html#removeNetwork(int)
for disconnect connection:
http://developer.android.com/reference/android/net/wifi/WifiManager.html#disconnect()
For updating netwrok
http://developer.android.com/reference/android/net/wifi/WifiManager.html#updateNetwork(android.net.wifi.WifiConfiguration)
For all other methods
refer to
http://developer.android.com/reference/android/net/wifi/WifiManager.html
Related
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
I'm calling the function WifiManager.addNetwork(WifiConfiguration) to try and add an adhoc wifi network to the device's wifi configurations. But on Android M this function returns -1, I'm assuming because it doesn't have internet access. It works fine on most other devices. Below is code snippet I'm using.
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = '\"' + ssid + '\"';
wifiConfiguration.hiddenSSID = false;
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(wifiConfiguration);
Any way to get around the internet connectivity check and force the network to be added?
You need to enable the network after that :
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();
enter code here
break;
}
}
I just started an app that must connect automatically to an open access point called "DEVICE AP".
I'm already getting a list with wifi networks, but I don't get "DEVICE AP" until I try the scan for three times (more or less).
So, my questions is: How can I scan continuously until I find "DEVICE AP"?
And other one, is it correct to connect to my open network by this way:
private void connectDeviceAccessPoint() {
String networkSSID = "DEVICE AP";
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + networkSSID + "\"";
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(wifiConfig);
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();
//Start service to send messages to device.
startService(new Intent(WifiActivity.this, SocketService.class));
doBindService();
break;
}
}
}
Thanks in advance!
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).
I am trying setup WiFi programatically on an Android tablet. If there is no WiFi connection then add the network with SSID and passkey read from a text file. Adding this network to network list and saving the passkey. It's adding the network and saving the passkey also but when I try to connect, it's not connecting. let me know...
public static void setupWifi(Context _context)
{
if(deviceConfig.wireless_ssid.length()==0) return;
WifiManager wifi = (WifiManager)_context.getSystemService(_context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + deviceConfig.wireless_ssid + "\""; //IMP! This should be in Quotes!!
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
wc.priority = 40;
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wc.preSharedKey = "\"" + deviceConfig.wireless_passkey + "\"";//
Log.d("ssid : ", wc.SSID );
List<WifiConfiguration> netWorkList = wifi.getConfiguredNetworks();
WifiConfiguration wifiCong = null;
if (netWorkList != null) {
for(WifiConfiguration item:netWorkList) {
if (item.SSID.equalsIgnoreCase("\"" + deviceConfig.wireless_ssid + "\"")) {
wifiCong = item;
}
}
}
if (wifiCong == null) {
boolean res1 = wifi.setWifiEnabled(true);
int res = wifi.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = wifi.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + b );
boolean es = wifi.saveConfiguration();
Log.d("WifiPreference", "saveConfiguration returned " + es );
}
}
Thanks
Kiran
I had this problem as well. The way I solved it was manually adding the wifi network to the device. I then enumerated over the configuration and copied the values that the device added to successfully connect to that network.
To clarify:
Add the network by hand, call the network manager to get the configured networks, find your network, and then call the getters out of everything you are trying to set - you will find the exact configuration you need.
void getWifiConfigs()
{
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> networks = wifi.getConfiguredNetworks();
for (WifiConfiguration current : networks){
//check getters here
}
}