In IOS we have implemented using Reachability framework which provided by apple, In Android We need to find whether active internet connection is available are not. Actually We need find whether Wi-fi connected with active internet connection or not, We have tried lot of ways but we need to deduct instantly. In this case we should find out host reachability status of internet connection. We have tried this below link .
It was taken some time for the response, But we need to get instant response to finish the requirement. We need to solve the case, device connected with Wi-Fi but doesn't have internet connection. If anyone know the solution help us. Thanks in advance.
In Android, you can subscribe to get a notification when it WiFi has connected. This is as instant as you are going to get.
ConnectivityManager will also allow you to poll to see if WiFi is available, but it's much better (and more battery friendly) to get an event.
See here for details
Related
I'm in the design phase of an Android application that will require a connection to multiple wifi networks, and I'm wondering if anyone can provide advice and/or code snippets regarding the best way to go about this. The app will have a connection to one wifi network that has internet access, and another wifi network with no internet access that will be used to share data accross the devices (raspberry pis) on it.
Can I be connected to both networks simultaneously? If so, how do I specify which network to use when making a request? I've been reading the Android 5.0 documentation which indicates its possible, but I'm a little confused on how to implement it.
If its not possible, this post from 2011 says that the WiFi Direct standard could be another solution. WiFi Direct is now supported on Android, but I can't find many examples of how to use it to achieve my goal.
Any advice is much appreciated!
As far as I know, you can have only one connection to the Access point, meaning that you can indeed make the device connected to the access point which offers you the internet connection, but then you can not connect to other networks.
Anyway, I would suppose that in your use case that is not the problem, actually I don't see any problems there. At least, if you simply have the connection and then have the Raspberry Pi's connected to you, i.e. you simply create a access point in your Android device, and all devices connect to it.
I wanna create an application using wifi direct.
In connecting step between 2 devices. I wanna set wifi direct on if wifi direct state of the device use my application is off. Can I change wifi direct state of the device(mobile)? How?
When i send request to connect to another devices Can my application auto accept the request without asking the device received request?
Please support me. Thanks so much!!!
The answer is yes, you can turn on/off the WIFI connection of the handset using the WifiManager:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
WIFI and wifidirect are enabled at the same time. You will need to add the needed permissions to the Android manifest.
Check Android Developers for more information on "http://developer.android.com/reference/android/net/wifi/WifiManager.html#setWifiEnabled(boolean)".
About accepting the connection without human interaction... It should be possible, but only if your app is the one handling the connection at the end-side and you have developed it in a way that will take care of all the steps. However this is not good from an end-user perspective since you are not giving him the power of decision. A nice solution would be doing it manually and allowing the user to decide if allow it automatically for the next time for that specific device.
Please, provide feedback if anything is not clear or does not really answers your questions.
I'm starting to write an Android application to perform background monitoring of my web server. With WiFi enabled a problem will be that traffic is directed over that by default, I'd like to always use 3G for the HTTP request to check external availability as well. I know I could use this code to disable WiFi programmatically:
WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
But as it will be running in the background it's a bit clumsy and will interrupt other operations over WiFi while the check is in progress. Looking at the Socket Documentation I can't see any apparent way to achieve this. I'm wondering if there's any supported way to achieve this on a non-rooted phone? I have full control over the server so the protocol doesn't specifically have to be HTTP.
This seems like it would be an awfully dangerous feature for Android to allow.
What about users who are on metered 3G plans? How would they feel if an app forced them to use a 3G connection and then bugged out or something and blew away their whole data quota without them even knowing it (thinking they were on WiFi)?
Also, there could not be more than one active networks at a time.
Possible Resolutions-
What you can do is to force disable the wifi network, when your application is active and then enable it later.
Also, Try searching for a method called requestRouteToHost. It allows you to specify the network type and the host you want to find a route to.
I don't believe you can do this in android since only one network connection is active at any given time. A similar question is posted here as well:
Send HTTP request through 3G network without using WiFi?
I have a service which polls every hour attempts to sync data automatically to the web from the device.
I have it working but I need to find a way to establish an internet connection if there is not one available.
For example - If my phone is in standby mode (screen locked for a period of time) the internet connection is dropped and it is unable to sync when it needs to.
I would like to attempt to connect via wifi if available, then using 3G if required.
Is there a reliable way to do this?
UPDATE -
I found an article online which uses this code to attempt connection via 3G if there is no wifi available :
int resultInt = connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableHIPRI");
unfortunately it doesn't seem to work on all devices, maybe I need to try other options than "enableHIPRI" ??
Any ideas?
OK, although I haven't been able to solve this problem I have discovered that the main cause for the delay in between sync attempts is because the cpu gets stopped during sleep mode, this means my timer task was paused..
My workaround was to rewrite the service using a wakeful intent to get around the timer issue.
This makes my solution much more reliable so I am closing this question! :)
You should be able to do most of that using ConnectivityManager. It allows you to query the available types of networks and using requestRouteToHost you should be able to ensure there's a connection set up that you need for syncing.
I think you cannot rely on such thing on mobile phone, as in any embedded device you cannot be sure if the device will connect and stay connected when you want. You can only leave a message for the user that data was not synchronized or try to minimize it by checking if there is connection and then synchronizing not just in one hour.
Like many apps my app depends on WIFI and Http Connection to operate. What I am not clear on is how I can time a wait until WIFI is enabled AND device is attached to WIFI network is. What is the best way to delay on application startup long enough for this to happen? during this time I would for example keep buttons deactivated. Also is there any way to make the device attempt to connect to its preferred network? Thanks
Take a look at ConnectivityManager and WiFiManager. Using these you can get events when network state changes
Use WifiManager to get the state of the connection.
Generally you will have to try. A Wifi connection can show as connected but not be able to actually send/receive because the signal is too weak. Once the connection is up, the app should try connecting and only when it succeeds, the buttons should be activated and so forth.