Android connect to WiFi without human interaction - android

I'm wondering if there are some code snippets that can be used to connect an Android device to a WiFi network. The network should be either open or WEP/WPA encypted, and visible to that device. Normally, we use GUI interface to input WiFi passwords and tap the connect button. I want to store the password in a place, and use the password to connect to the network seamlessly without human interaction. Is that possible? Thanks a lot.

Thanks guys. With your help, I'm now able to connect to a WPA/PSK encrypted network without pain. Here is my code snippet:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// setup a wifi configuration
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"YOUR_SSID\"";
wc.preSharedKey = "\"YOUR_PASSWORD\"";
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
// connect to and enable the connection
int netId = wifiManager.addNetwork(wc);
wifiManager.enableNetwork(netId, true);
wifiManager.setWifiEnabled(true);
The tricks are :
SSID string should be surrounded with ", which is denoted by \"
addNetwork() method DISABLES the added network by default, so you should enable it with the enableNetwork() method.

To make OPs sample code work, I had to add one more line:
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
Without that line it just won't connect to the network. The configuration is accepted and added, but no connection attempts are made. I actually got the following message in the logcat window:
Event [WPA: Failed to select WPA/RSN] android
which put me to the final solution, figuring out why it didn't work for me.

WifiManager - Have you tried looking here. The addNetwork() method looks like it can do what you want it to do. All you have to do is put the information in a WifiConfiguration class and then add the network, then enable that connection. The Documentation is all there.

Checkout the documentation for "WifiManager"
It can be used to enable wifi:
WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
And it can be used to do many other things.
Edit: Don't forget to update your permissions when monitoring and changing wifi state, example:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
etc...

Related

Android blacklist wifi bssid

I have a problem banning a specific bssid. I can see there is a class called WifiNative that was in api 19 but not able to access it:https://android.googlesource.com/platform/frameworks/base/+/kitkat-release/wifi/java/android/net/wifi/WifiNative.java
Is there any other way anyone knows of to do this? Maybe via JNI? Any android system libraries that can be used? c,c++?
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
newWifiInfo = wifiManager.getConnectionInfo();
from there you can get the BSSID by doing
newWifiInfo.getBSSID()
then "blacklist" by turning off the wifi via
wifiManager.setWifiEnabled(false)
Also don't forget your permissions
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
If you want more control over your wifi state, you can use the following library to "sniff" your connection and other things
https://github.com/pwittchen/ReactiveNetwork

Does Android WifiManager saves the Config even without saveConfiguration()

I am developing and app for a restaurant. They want their customers to connect their Wifi via app.
The code:
WifiManager mManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = new WifiConfiguration();
//Asume I added things to config.
int id = mManager.addNetwork(config);
mManager.enableNetwork(id, true);
So even if I don't call
myManager.saveConfiguration();
can the device connect that Router without my app?
Thank you for your time.
OK! I read about it more and more, I found the answer. And also the solution.
So, you need to use use
mManager.removeNetwork(id);
after it is disconnected. I assume you saved your BSSID's in somewhere. Also save the ID that you got. With a Timer task working at background check the current BSSID, if it is not the same, remove the ID from Configured Networks as above.
Here is how you check current BSSID:
WifiInfo wifiInfo = mManager.getConnectionInfo();
String currentBSSID = wifiInfo.getBSSID();

Android WiFi Connection Info

I'm getting wifi connection info this way:
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
But, could getConnectionInfo method return null in any case? Android documentation reference say this:
Return dynamic information about the current Wi-Fi connection, if any
is active.
I guess if the device is not connected to any network it will return null, won't it?
Thanks
Yes it will return null, if it is not connected to the wi-fi.
But if you are also checking whether internet is available by using above code, then it is not 100% correct way to do so.
Cause it may happen that your device is connected to wifi but no internet is available. so for that
you have to ping any live site (such as google,facebook, or any other trusted website) and check if the returned status code is 200.
I hope this helps.

Transmitting small amounts of information using WiFi SSIDs?

Is it possible to allow 2 Android devices to communicate by using SSIDs? One device would broadcast a message by creating a hotspot with the message as its SSID. Nearby devices can read and interpret the message. I couldn't find anything in the Android documentation that allow this. Is it possible on Android devices and how would I do this?
You can use this call to create a hotspot:
private boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled);
...
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "\"SSID_NAME\"";
// more configs here.
And to scan wifis around something like:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<ScanResult> results = wifi.getScanResults();
You can read the wifi info from there. Maybe you can identify the hotspot by MAC address.

Android: Get SSID and password of a phone which is a portable wifi hotspot

I need to find a way to get the SSID of the network that my Android device is shared (My device is being a portable wifi hotspot).
Not the SSID of the network I connected to.
thanks
I finally found that there are some hidden method about getting/setting wifiAP mode in android WifiManager class. In WifiManager class, getWifiApConfiguration could be invoke to get the configuration in which the SSID of wifiAp could be found.
I know OP has already answered his/her own question, but it wasn't too clear for me, so I'll just post my complete solution here for future people that got here:
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method[] methods = wifimanager.getClass().getDeclaredMethods();
for (Method m: methods) {
if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = (WifiConfiguration)m.invoke(wifimanager);
String ssid = config.SSID;
String bssid = config.BSSID;
//or other info about the current hotspot, accessible through config
}
}
Note: you would need the following permissions:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Categories

Resources