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?
Related
In my application I have to get notified whenever the device connects or disconnects from a WIFI network. For this I have to use a BroadcastReceiver but after reading through different articles and questions here on SO I'm a bit confused which Broadcast action I should use for this. In my opinion I have three choices:
SUPPLICANT_CONNECTION_CHANGE_ACTION
NETWORK_STATE_CHANGED_ACTION
CONNECTIVITY_ACTION
To reduce resources I really only want to get notified whenever the device is CONNECTED to a WIFI network (and it has received an IP address) or when the device has DISCONNECTED from one. I do not care about the other states like CONNECTING etc.
So what do you think is the best Broadcast action I should use for this? And do I have to manully filter the events (because I receieve more then CONNECTED and DISCONNECTED) in onReceive?
EDIT: As I pointed out in a comment below I think SUPPLICANT_CONNECTION_CHANGE_ACTION would be the best choice for me but it is never fired or received by my application. Others have the same problem with this broadcast but a real solution for this is never proposed (in fact other broadcasts are used). Any ideas for this?
You can go for WifiManager.NETWORK_STATE_CHANGED_ACTION works.
Register receiver with WifiManager.NETWORK_STATE_CHANGED_ACTION Action, either in Manifest or Fragment or Activity, which ever suited for you.
Override receiver :
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
boolean connected = info.isConnected();
if (connected)
//call your method
}
}
Please try
#Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction("android.net.wifi.STATE_CHANGE");
registerReceiver(networkChangeReceiver, filter);
}
#Override
protected void onDestroy() {
unregisterReceiver(networkChangeReceiver);
super.onDestroy();
}
and
BroadcastReceiver networkChangeReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (!AppUtils.hasNetworkConnection(context)) {
showSnackBarToast(getNetworkErrorMessage());
}
}
};
I am using this and it is working for me. Hope it will help you out.
I have a socket that connects to a TCP server using either Wifi or 4G signal that receives a PING every 5 minutes from the server. But if I turn Wifi off on the device, I would like the socket to detect this and reconnect using a 4g signal.
I was wondering if there was a way to reconnect the socket automatically to 4G if I go out of the range of the wifi or if I turn wifi off?
Also is there a way to go the other way as well(From 4g to wifi if the wifi is available?
Register an inclass BroadCastReceiver to listen to WIFI on or off :
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.wifi.supplicant.CONNECTION_CHANGE");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
boolean isConnected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
if (isConnected ) {
//Reconnect using Wifi.
} else {
//Reconnect using 4G.
}
}
}
registerReceiver(receiver, filter);
Dont forget to unregister the receiver:
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
Regarding the EXTRA_SUPPLICANT_CONNECTED extra, the documentation states:
The lookup key for a boolean that indicates whether a connection to
the supplicant daemon has been gained or lost. true means a connection
now exists. Retrieve it with getBooleanExtra(String, boolean).
Constant Value: "connected"
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
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();
}
}
I want to receive an interrupt when the android device starts charging either thr USB or AC
I have registered the receiver through the following code--
registerReceiver(powerMonitor,newIntentFilter("android.intent.action.ACTION_POWER_CONNECTED"));
The code which accepts the broadcast is--
public void onReceive(Context context, Intent intent) {
powerMonitor = new BroadcastReceiver() {
public void onReceive(Context context,Intent intent) {
if (intent.getAction().equals("android.intent.action.ACTION_POWER_CONNECTED"))
{
msg = "power connected";
}
The issue is when I connect the device through a USB cable I fail to get the interrupt and if I try to remove and plug the USB cable,the VM gets disconnected..How do I check it?
Have I gone wrong?
I even tried d following code--
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(broadcastReceiver, filter);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
if(plugged==BatteryManager.BATTERY_PLUGGED_AC ||plugged==BatteryManager.BATTERY_PLUGGED_USB)
{
msg="power connected"
}
Please help!!
please have a look on this link (The answer given by me)
activate an application when a power button is clicked
this will help you to solve your problem.