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();
Related
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.
I am working on an app that would allow indoor navigation using hardware devices that emit wifi signals in large public places such as hotels and hospitals. Each location would have multiple devices and each device would be unique so that we can identify the exact location of the device/person.
For this I don't want the user to connect to the wifi, if only a scan of the network was possible which could determine the current wifi beacon the mobile is closest to would do the job. I decided to use Android's WifiManager for the task:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
wifi.startScan() ;
List<ScanResult> mScanResults = wifi.getScanResults();
String s = "" ;
for(ScanResult result: mScanResults){
Log.d("Wifi", result.SSID) ;
}
None of the attributes for result seem to be appropriate for use as a UID for Wifi Beacon, I have two questions:
a) Can we use something like a BSSID for this purpose?
b) Can I write my own wifi manager class and then somehow negotiate a handshake with the wifi where it just tells me who it is without connecting? If so please share resources for doing the same.
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.
I'm not asking about how to create a WIFIConfiguration
that I want to connect to.
But I'm asking how to set my own Device SSID for my own phone?
private WifiManager obWifiManager;
obWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = obWifiManager.getConnectionInfo();
sendMessage(wifiInfo.getSSID());
The above code has no setter method.
Only getter method. Please help!
I then stumbled upon this article. it said the configuration file is located almost there for any linux OS. There is an SSID setting over there!
So i search on that location but found none!
Probably android put it in a different order / place. Any clue?
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...