Detect when WiFi network fails to authenticate - android

I am trying to connect to a WiFi network using WiFiManager. Here is the code I use to do so :
WifiManager manager = [...];
WifiConfiguration config = new WifiConfiguration();
manager.setWifiEnabled(true);
//Set SSID, PSK, etc...
[...]
//Connect to the network
int netId = manager.addNetwork(config);
if (netId == -1)
{
Toast.makeText(this, "Invalid PSK", Toast.LENGTH_LONG).show();
return;
}
manager.enableNetwork(netId, true);
manager.saveConfiguration();
Beforehand, I registered a BroadcastReceiver for the WifiManager.NETWORK_STATE_CHANGED_ACTION action. I'm trying to detect when the connection succeeds or fails (for various reasons including authentication) by checking the DetailedState of the given NetworkInfo.
Everything works when the connection succeeds, but for some reason my receiver isn't "executed" when the connection fails. The network is added to the device's list, I can see it by going into WiFi Settings, but it does not try to connect (or it does try to connect but doesn't notify my receiver).
Any clue ? Thanks !

I finally managed to achieve what I wanted.
To detect that the connection succeeded, register the WifiManager.NETWORK_STATE_CHANGED_ACTION broadcast intent and check for the NetworkInfo.DetailedState.OBTAINING_IPADDR detailed state (for WiFi obviously).
To detect that the connection failed, register the WifiManager.SUPPLICANT_STATE_CHANGED_ACTION broadcast intent and check for any WifiManager.EXTRA_SUPPLICANT_ERROR integer extra (typically WifiManager.ERROR_AUTHENTICATING). You may need to gain access for Android hidden API to achieve that.

The enableNetwork method return true if the connection status is success.
This works fine on success but is not the best method to check the failed status (return true if the connection failed after the first step authentication).
I think that you can check in the networkInfo object the actual connected network to see if the request is ok:
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

Related

what does "isRoaming" in the Wifip2p networkinfo mean?

What is the meaning of isRoaming in Wifip2p network info mean?
[http://developer.android.com/reference/android/net/NetworkInfo.html#isRoaming()]
It tells that usage of data on that network may incur extra charges? Which data is it refering to and how the usage will lead to extra charges?
Well, if you're outside your home country/state and you turn on the phone it will automatically establish a GSM connection to the roaming partner network -> TelephonyManager.isNetworkRoaming() will return true. If data roaming is disabled or 'use only 2g' is enabled NetworkInfo.isRoaming() will return false, because no data connection is established. If you switch data services on NetworkInfo.isRoaming() will return true as well, since now both (GSM and data connection) are established and in roaming mode.
Hence, setting data-roaming on/off will make NetworkInfo.isRoaming() return true/false.
UPD: It is used when one wants to know whether the user has enabled Data on Roaming while on 2G/3G network.
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
//[edit: check for null]
if(ni != null) {
//...Here check if this is connected and available...
if (ni.isRoaming())
{
// user has enabled data even while he is on Roaming!!
}
}
Since he asked specifically about Wifi P2P the correct answer that this will be an unused field as roaming only applied to cellular network and does not apply to Wifi P2P network information.
The only fields of networkinfo you should be using concerning wifi P2P is: isConnected (after which you normally request the connection info from the wifip2pmanager).

Android is device connected to the internet method

I want to create a method which tells if the device is online. As far as I understand ConnectivityManager tells only if the device is connected to a network. This doesn't mean that the device is connected to the internet. To ensure that the device is online I'm using InetAddress.getByName("google.com").isReachable(3); but I can't use it on the main thread. I can create a separate thread to check the connectivity and then use a callback function but is there another way? I don't want my app to do anything before it is connected. Do you have any solutions? Thank you!
With any networking, there isn't a guaranteed way to check whether or not you are connected to an endpoint without actually sending data. Even if the device is connected to a network, has an ip address, recently received data, e.t.c, it doesn't mean that you still have a connection.
I would suggest allowing the user to progress into your application as much as possible, queuing up the requests to your server in the background whilst a connection is established. Use the same framework to resend data if the connection is lost whilst the user is using the app. Make the connection to the server as transparent to the user as possible, unless it fails to connect after ~1 minute
try this:
public static boolean isOnline(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}

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);

Android - Execute an http post on wifi connection

I'm working on an app that logins on a public wifi network. I've got the login working by doing a http post to the authentication server.
My next step is making the app do the http post automatically when there's a new wifi connection, checking that we are connected to the correct ssid.
My question is, which is the best way of doing this? Is the best way to create a service and registering to a broadcastreceiver?
Thanks for your help
The way I do it is that i have a broadcastreceiver that listens to android.net.conn.CONNECTIVITY_CHANGE intents. In that broadcast receiver you can check what kind of connection is made by polling the ConnectivityManager:
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean hasWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null && cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isAvailable() && cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
And then start the service doing the post to the auth server.
Alternatively you could indeed let the service listen to android.net.conn.CONNECTIVITY_CHANGE intents and do the wifi check there.

Android terminate wifi ability within application

One of the major requirments of my application is using cellular internet connection only.
In case of detecting wifi connection, the application have to disable the wifi ability and connecting through the cellualr internet connection.
How it can be done?
Thanks,
Eyal.
Checking WIFI connection
You can use the following code to check if the active network (if any - do some null checks here) is of type WIFI.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connManager.getActiveNetworkInfo();
if (netInfo!=null) {
// detect type using netInfo.getType()
}
There's a TYPE_WIFI int constant in ConnectivityManager that you can use for your check. (getType returns an int to identify the network type).
More info on the Activity Manager can be found on the Android dev site.
Disabling WIFI
n order to disable the WiFi state, you have to grant the following permission in the application manifest
android.permission.CHANGE_WIFI_STATE
You can then use the WifiManager to set enable/disable the WIFI.
WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
More info on http://developer.android.com/reference/android/net/wifi/WifiManager.html
For a complete sample, check the following post : http://android-er.blogspot.com/2011/01/turn-wifi-onoff-using.html

Categories

Resources