I am using this code to disconnect from a Wifispot:
if(getActivity()!=null) {
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
wifiManager.disconnect();
}
WiFi disconnects and Settings show Wifi On, the wifi I was connected is shown as
Saved, secured with WPA/WPA2
That's perfect. The problem is that I get out of range and return into the range again of the WiFi, it doesn't "reconnect" automatically. Which code should I add to automatically reconnects WiFis disconnected by WifiManager?
When you set up your wifi configuration.
add this :
mWifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
for more information http://developer.android.com/intl/es/reference/android/net/wifi/WifiConfiguration.html
Hope this help!
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 know how to determine if a wi-fi connection is the active connection. However, can someone tell how to determine which wi-fi network is the current active?
Use WifiManager.getConnectionInfo(). This returns a WifiInfo object which contains all the info you need about the active wifi connection; in particular, WifiInfo.getSSID() will give you the SSID.
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
String BSSID=wifiManager.getConnectionInfo().getBSSID();
I'm trying to block the wifi connections. I want my application to turn on the wifi, but does not connect to any network that is already stored on the smartphone. But even after I use the SCAN_ONLY mode, he continues to connect to networks that already "know".
.....
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
.....
wifiManager.setWifiEnabled(true);
WifiLock scanOnly = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "scanOnly");
scanOnly.acquire();
Already in despair i tried to disconnect after to make sure that the state is WIFI_STATE_ENABLED wifi. That the app can not connect for a few seconds, but after some time it connects to a network in the same ...
wifiManager.setWifiEnabled(true);
....
WHEN (WIFI STATE == WIFI_STATE_ENABLED)
{wifiManager.disconnect();
scanOnly.acquire();}
Can someone help me?
Tks
WifiLock not for this. Only for "Allows an application to keep the Wi-Fi radio awake". This does not forbid anyone to connect to wifi. Only says "I want to be able to do at least this in sleep mode".
You have to do what you said Peanut above: get the list of all the stored networks and then disable wifi in BroadcastReceiver.
#Override
public void onReceive(Context c, Intent intent) {
saveWifiList(mainWifi.getScanResults());
unregisterReceiver(this);
mainWifi.setWifiEnabled(false);
showWifiList();
}
One way would be to get the list of all the stored networks and then disable them. You might want to store their state somewhere so you can restore the initial state after you're done.
I am writing a program about the communication between android device and PC.
Is there any way to get the MAC address of BLUETOOTH or WiFi of an android device, when the Bluetooth or WiFi is turned OFF?
If so, how?
this works for me with wifi on and off i not try the bluetooth
WifiManager wimanager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
String address = wimanager.getConnectionInfo().getMacAddress();
Log.d("TOKEN", address);
Yes, you can get MAC addresses even when Bluetooth / WiFi is OFF.
Getting bluetooth information is as easy as this:
BluetoothAdapter.getDefaultAdapter().getAddress(); // MAC address
BluetoothAdapter.getDefaultAdapter().isEnabled(); // true if ON
No need to use Context, yay!
And to complete the answer.. WiFi state:
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.getConnectionInfo().getMacAddress(); // MAC address
wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED; // true if ON
For a long time my device would always auto start WIFI now it does not. Could someone show how to turn wifi on automatically when device boots? Also if my app sleeps sometimes on recovery wifi is gone? Show how to test if wifi is active and if not active how to turn it on programatically. Thanks
Use wifi manager class and use the following code to test wifi connectivity
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled())
{
// write your code
}else{
wifi.setWifiEnabled(true);
}