Detect nearby wifi without connecting to it - android

I want my app to "do something" when a wifi is nearby. But the phone doesn't connect to it. I don't have the password. For example, if the wifi University0001 is reachable I would like to silent my phone but I don't have the password to University0001.
Is there a way to have a broadcast receiver that triggers when new (not connected) ssid are around?
I would not like to have a "timer" to periodically check new SSIDs. I am asking for a receiver to trigger when new networks are around and maybe then check for the one I want.
I hope to be clear.
Thanks

Is there a way to have a broadcast receiver that triggers when new (not connected) ssid are around?
yes there is.
if your WIFI is on, and you are not currently connected to a WIFI network - the system periodically scan for available access points. when it will detect new visible access point it will send the SCAN_RESULTS_AVAILABLE_ACTION broadcast.
you can register to that broadcast, and when it receives - get from WifiManager the scan results:
#Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiManager (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> scanResults = wifiManager.getScanResults();
}
if you are already connected to a WIFI network - then you won't have any choice but to trigger the scan yourself periodically...
be careful:
performing from this receiver long operations (such network requests..) any time it receives is a bad approach - you'll drain the users battery very fast.

Related

How to get the progress of a conecting to wireless connection

I'm making a app that is displaying the available wireless networks and a user can choose one network and connect to it.
I have a broadcast receiver and I receive the status of the connection :connected , when the connection is done , but I would like also to display more info to the user like , Authenticating, Obtaining IP address , cause now I only know when network is connected but noting till then.
Any idea how to do this?
Thanks you very much
Fine-grained connection state is available from the NetworkInfo object - getDetailedState
NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
DetailedState state = ni.getDetailedState();
You also need to register your broadcast receiver to receive state change events from WifiManager
IntentFilter filter =
new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
filter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);

intent filter which can used to listen mobile data enable and disable event

Actually In my app I have to show the toggle button on/off if mobile data packect on/off.
But there is a problem my local boradcast can't listen mobile data packet on/off event if my WiFi is already on. Actually as I understand If my WiFi on then mobile data packet enable and disable event never affect connectivity status. Thats why my some intent filter never works.
android.net.conn.CONNECTIVITY_CHANGE
I need a intent filter which is similer to android.net.wifi.WIFI_STATE_CHANGED so that whenever mobile data packet on or off event fire my broadcast can listen it easily. And I can change my widget.
I dont know of any intent-filter that we could receive when the mobile data is turned on or off but the following code is to determine whether mobile data is turn on or off.
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected())
//mobile data is on
else
//mobile data is off
What you can do is write a service of your own that will constantly run the above code. When the mobile data is on, you can send a broadcast to your activity telling the same. Same goes when the mobile data is off.
Edit1:-
This works even if wi-fi is connected.
Edit2:- Don't forget to add ACCESS_NETWORK_STATE permission while using this
I think in your app,you need to import WifiManager Class.
WifiManagerclass provides the primary API for managing all aspects of Wi-Fi connectivity. Get an instance of this class by calling
Context.getSystemService(Context.WIFI_SERVICE)

Android loses Network connection and it stays disconnected

Im am working on a App that has to work in the Background, and it has to send Geolocation. It is allready in production and we sometimes have the problem that an App just stoppes sending information.
It seems that only a restart of the Phone helps.
Now I have to fix this somehow, it seems to me that resetting the Networkstack would solve the problem. My question is this, how can I programticlly reset the network connection? Or does anybody have any other suggestions for this problem.
how can I programticlly reset the network connection? Or does anybody
have any other suggestions for this problem.
You should not go for start a network connection programatically. I would suggest you to check for network connection before posting the location to server, and if device is disconnected show the notification to user to enable network connection.
Why I am not in favor of set network connection by code
This functionality can cost the user. So raise a notification and let the user to start network connection.
And yes if user dosn't start the network connection your application will not post location any more.
Update for Listen device network state
Register a Receiver to listen for CONNECTIVITY_ACTION
For example:
BroadcastReceiver sampleNetworkStateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.w("sampleNetworkStateReceiver", "Network State has Changed");
//For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY is set to true if there are no connected networks at all.
// So you need to get EXTRA_NO_CONNECTIVITY extra from intent.
// that will tell you if device has loses the conectivity.
boolean noConnectivity =
intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (noConnectivity) {
//TODO: Task for Device has no connection
} else {
//TODO: Task for Device has connection
}
}
};
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(sampleNetworkStateReceiver, filter);
Ref : NetworkConnectivityListener and Determining and Monitoring the Connectivity Status

Check Network Coverage in Android before trying to send SMS

I want to create an application for Android smartphones that checks if the phone is in Airplane Mode. If it is, the application gets the phone off the Airplane mode and checks if there is any network connectivity to send a SMS. When I say network connectivity, I mean mobile phone network coverage to send the SMS, I don't want to check for internet connectivity. If there is network connectivity the application will try to send a SMS.
I have managed to do the Airplane Mode check and toggle, but I don't find a way to see if the phone is connected to the cellular network and if there is coverage. I found many examples that check for internet network connectivity, but is not what I need.
Is there any way to check if the phone is connected to the cellular network and if there is coverage to send SMS?
Thanks in advance for your help.
Use this to check the signal strength:
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener signalListener = new PhoneStateListener() {
public void onSignalStrengthChanged(int asu) {
//asu is the signal strength
}
};
telephonyManager.listen(signalListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTH);

Android WifiLock WIFI_MODE_SCAN_ONLY not working

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.

Categories

Resources