Android :: How to disconnect from a wifi network? - android

I have Googled and find many sites saying about 'disabling Wifi radio'. But in my case, I just want the android device to disconnect from a specific wifi network(SSID preknown) without switching OFF the WiFi radio. Please give me some insights on this issue

Wow this shouldn't have taken a month to be answered.
Here's the easiest way that I usually use:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.disconnect();
discon = new DisconnectWifi();
registerReceiver(discon, new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION));
Where DisconnectWifi is just a small class the extends BroadcastReceiver:
public class DisconnectWifi extends BroadcastReceiver {
#Override
public void onReceive(Context c, Intent intent) {
if(!intent.getParcelableExtra(wifi.EXTRA_NEW_STATE).toString().equals(SupplicantState.SCANNING)) wifi.disconnect();
}
}

Related

Search available wifi networks in sleep mode in android

I want to check available networks bssids "just check no connecting" for that i use this code
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(myrec, filter);
------------------------
private BroadcastReceiver myrec=new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(tag+" myrec","scan complete");
}
};
its work fine in many phone i tested but in galaxytab4 "smt231" in sleep mode not working and in normal mode take too long time to complete scan "more than 2 min" but when i go to settings>wifi my brodcast registered right away.
can anyone help me about this problem.
update
i found wifilock and now my code is
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
_wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, this.getClass().getName() + ".WIFI_LOCK");
_wifiLock.setReferenceCounted(true);
if(!_wifiLock.isHeld()){
_wifiLock.acquire();
}
wifiManager.startScan();
-----------------
private BroadcastReceiver myrec=new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(tag+" myrec","scan complete");
}
};
but still not working in sleep mode.
and i am sure about running my code but scan reciver broadcast never going to run in sleep mode.
intersting thing is when you turn your wifi on even when your phone is in sleep mode when your going to range of known wifi network its going to connect to network and get notifications like emails. so its possible to check for wifi networks in sleep mode the thing is how?

Alert user when wifi network has changed

I am working with Android. I have a service that is getting data from a wifi network. I would like to alert the user if they lose connection to that wifi address. I have been trying to work it out, and I think I have narrowed it down to using a BroadcastReceiver. Here is my implementation so far...
WifiReceiver receiverWifi;
protected void onCreate(Bundle savedInstanceState) {
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.NETWORK_STATE_CHANGED_ACTION));
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
//Does this get executed when the network changes?
}
}
Specifically, my question is, how can I edit my code to run in my service that will let the user know through a notification that the wifi they were connected to was lost or was changed? Thank you for any help.

Detect BSSID changes Constantly

I am developing an android application that is supposed to constantly check the BSSID of the access point to which the phone is connected, knowing that the phone will always be connected to one network but may connect to multiple access points (Not at the same time, of course) since the building is big. I used a BroadcastReceiver but to check whether the phone got connected to a different access point, I have to close the application and run it again to get the updated BSSID. I want the application to constantly check and update the UI with the new BSSID without me having to close the application and open it again. Do you have any idea how I can do that?
Thank you
I have sucessfully used this in the past to monitor for WiFi connection changes:
public class NetworkChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
// etc
}
}
}
Just register your receiver in onResume() and unregister it in onPause() and you should be good to go.
#Override
protected void onResume() {
super.onResume();
IntentFilter connectivityFilter = new IntentFilter();
connectivityFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(your_broadcast_receiver, connectivityFilter);
}

Android - Correct way to detect disconnecting from a particular wifi ssid?

I've seen a couple of BroadcastReciever examples to detect wifi disconnects but none of them seem to work correctly (triggering twice for each disconnect for example) and none mention checking against an ssid, is this even possible?
So just to clarify, I want to detect disconnection from a particular ssid. An actual disconnect and not wifi being disabled on the device.
Thanks
EDIT: Re-opening as nothing works on both the devices we have to test.
NETWORK_STATE_CHANGED_ACTION was the answer in the end. The device having the problem registering this event started working when another app (which would also be listening for similar events) was uninstalled! No idea how or why an app could block events registering with another app. The final solution ended up being;
String action = intent.getAction();
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
{
WifiManager manager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
NetworkInfo.State state = networkInfo.getState();
if(state == NetworkInfo.State.CONNECTED)
{
String connectingToSsid = manager.getConnectionInfo().getSSID().replace("\"", "");
WifiStateHistory.recordConnectedSsid(connectingToSsid);
//connected
}
if(state == NetworkInfo.State.DISCONNECTED)
{
if(manager.isWifiEnabled())
{
String disconnectedFromSsid = WifiStateHistory.getLastConnectedSsid();
//disconnected
}
}
}
Are you sure there are twice notification for same state? There are always two phase of disconnection:
WifiManager.WIFI_STATE_DISABLING
WifiManager.WIFI_STATE_DISABLED
My code to detect (and rebroadcast) connections and disconnects (not by disabling wifi) and including the SSID as an extra is as follows. Most of what I've read suggested using SUPPLICANT_CONNECTION_CHANGE_ACTION but this just did not work correctly, it would seemingly only fire when disabling/enabling wifi on my device (Nexus 4) and not during connections. The only problem is on first run of the app as it won't record the current ssid so doesn't know what the ssid of the network that has just been connected. Any ideas around this?
public class EventMapper extends BroadcastReceiver
{
private static String lastConnectedSsid = "";
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION))
{
SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
if(state == SupplicantState.COMPLETED)
{
WifiManager manager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
lastConnectedSsid = manager.getConnectionInfo().getSSID().replace("\"", "");
Intent newIntent = new Intent();
newIntent.setAction(Event.App_Event_WifiConnected.name());
newIntent.putExtra("App_Events_SSID", lastConnectedSsid);
context.sendBroadcast(newIntent);
}
if(state == SupplicantState.DISCONNECTED)
{
boolean wifiEnabled = ((WifiManager)context.getSystemService(Context.WIFI_SERVICE)).isWifiEnabled();
if(wifiEnabled)
{
Intent newIntent = new Intent();
newIntent.setAction(Event.App_Event_WifiDisconnected.name());
newIntent.putExtra("App_Events_SSID", lastConnectedSsid);
context.sendBroadcast(newIntent);
}
}
}
}
}
I got the same problem in some custom roms. I used "android.net.wifi.STATE_CHANGE" to listen the network change. In the receiver, I used "(NetworkInfo)intent.getParcelableExtra("networkInfo")).getState()" to get the network state. There are three states: DISCONNECTED, CONNECTING, CONNECTED. You can use DISCONNECTED to detect if the network is disconnected.
Please let me know if it works in your situation(HTC One X (4.1)).

Android: Continuously seek for a specific WIFI Connection since boot completed

How can I check if a specific wifi connection is availible say for example SSID: Saaram01 is available. Whenever it is available I get notified.
I have done all this work through a button, like if you click the button it notifies if Saaram01 is available overwise does nothing.
The problem or the question basically is how can I check for this SSID availability everything 24/7.. Obviously for this I cant use a background service.. So is there anyother possible way to do it ?? Like using broadcast reciever or anything else ?
Any help will be highly appreciated !
Thank You.
IntentFilter i = new IntentFilter();
i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent i) {
// TODO Auto-generated method stub
ScanWiFiActivity a = ScanWiFiActivity.instance();
WifiManager w = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> l = w.getScanResults();
a.Clear();
for (ScanResult r : l) {
if(r.SSID.equals("saaram01") {
//Perform your logic here
}
}
}
};
registerReceiver(receiver, i);
n the FOR block work your magic and take action when you identify your network by SSID or BSSID

Categories

Resources