I'm doing a network App using tun as virtual network device on Android. I close other available interfaces such as eth0 (for WLAN) and rmnet0 (for 3G/GPRS) but keep my tun0 device alive and add a rule in route table
ip route add default dev tun0
so I can redirect all internet transfer into tun device (what I'm doing here is the same as OpenVPN). And then I saw the data transferring when I ping other IP. Browser and some other Apps work fine.
But some of Android Apps will check the Connectivity by ConnectivityManager before accessing netwrok. Since I close WIFI & 3G/GPRS, ConnectivityManager considers there is no connection, those Apps won't access network anymore.
So my question is:
How to CHEAT android ConnectiviyManager, make it believe that there is still an available connection (for any type)?
I don't think you can do this, and you wouldn't want to, it's a system-wide service. Imagine if your app relied on internet connectivity, but some malicious app was able to tell you there was and make your app unable to complete it's actions. It could cause a serious problem.
If your app needs to believe there is a connection in some states, I suggest you interface the connectivity / network classes, you can read the true state but then return whichever value you deem fit to your app
Related
I have a chip with a WiFi module on it, which acts as an access point. What I'm trying to do is connect to the AP (which has no Internet access), send a POST request to a local address (http://192.168.4.1/address) and receive a response from the chip. I am writing an Android application, which is supposed to do that (and it does most of the time).
The problem is, I have a test device, which has this optimizing feature and checks if the WiFi network you're connected to has Internet access and if not, it uses mobile data automatically. I don't want that so I would want to either "force" the application to send it through the WiFi interface or find a workaround.
I use HttpURLConnection at the moment and it works like a charm if I turn off my mobile data. Otherwise it just waits and at the end triggers the timeout.
I have searched a lot about this issue and so far I have found nothing.
Since Android 5 (API 21), you can force connections to use the WiFi even if it's not the default network.
One solution is to find the corresponding network, for example with ConnectivityManager.getAllNetworks() and ConnectivityManager.getNetworkInfo():
Once you have the Network, you can either :
Open a connection on this network using Network.openConnection().
Bind the application to the network with ConnectivityManager.setProcessDefaultNetwork or ConnectivityManager.bindProcessToNetwork() (API 23+)
See Connecting your App to a Wi-Fi Device (especially Routing network requests) for more details.
Before I start this journey in Android I want to know if it is possible to connect to a specific network SSID when an app is active and restore the previous network settings when exit the app or when the device go sleep.
The reason I want to this:
An app for a specific device that can be reachable via adhoc
connection;
No need to hook into an existing network (adhoc device needs no
config), so user friendly;
The app can use the all of the features of WIFI;
Device is isolated from the internet and provides own network
The idea:
The app discovers the available WIFI-networks when no device is
configurated yet;
The device can be found via an SSID prefix, for example 'umix_' and user can select the unique correct name after the prefix, for example: 123456 (= umix_123456);
Once successfully connected, this name will be used to connect;
User can use the device via WIFI;
When exit the app or device go to sleep it reconnects to previous network;
When the app is active on wake up, it reconnects to the device it's network.
Maybe the described behaviour looks like a bluetooth connection, however, it is not because it is WIFI, better bandwidth for example and server facilities. However, the easy of use for it's user is similar to bluetooth, I want to achieve this.
Because I cannot find any info on this on the net, I ask it here. Is what I want possible anyway? Any directions, links, code examples?
I have an Android device that has a WIFI adapter but no screen and no input devices. I want to use it as a silent server in my local network that runs / offers some services and will eventually be configurable via the internet. The service app will be pre-installed and launch upon boot. The problem is the initial configuration (mainly join the local WIFI network).
My idea is: Develop a Java GUI app that is used for the initial setup via USB.
Let's assume I get the desktop app to find and communicate with the Android service via the ADK (I will be happy to share here once I succeed). That way I will be able to use my screen & keyboard to configure it. Now the problem is:
How can I (on the Android device) obtain a list of WIFI netorks that the Android device finds and how can I tell it to connect to a selected network (with a provided password)?
Is there maybe a better way to achieve what I am trying to?
The WifiManager class should be helpful:
This class 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). It deals with several categories of items:
The list of configured networks. The list can be viewed and updated, and attributes of individual entries can be modified.
The currently active Wi-Fi network, if any. Connectivity can be established or torn down, and dynamic information about the state of the network can be queried.
Results of access point scans, containing enough information to make decisions about what access point to connect to.
It defines the names of various Intent actions that are broadcast upon any sort of change in Wi-Fi state.
This is the API to use when performing Wi-Fi specific operations. To perform operations that pertain to network connectivity at an abstract level, use ConnectivityManager.
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'm wondering if, with the G1, it's possible to force the network traffic to pass through the Wifi or through the Cell GSM network. I need to force an application to connect through the tower network to get some login information. Is this possible? Anyone have any ideas as to how this would be possible? I'm trying to accomplish this inside an app with the Android SDK (Sorry I wasn't more clear about that originally)
It turns out to be possible using the WifiManager object to disable and then re-enable the connection. You can block it for the duration of a network call if you want to force data over the cell network. I imagine there is a similar GSM network manager out there which will do a similar task for the cell connection.
I'm assuming that you can simultaneously communicate via either channel, so at some level there aught to be separate network interfaces for each. Since android is based on linux, you could try using ifconfig or a library function to determine which interfaces are available, and which does what. If you can accomplish this, there should be a way to explicitly send/recv via the channel you want (i.e. by communicating via the ip for that interface).
Sorry I couldn't be more specific.
What you are looking for on the G1 is to set the default route. You may do so using the linux utility aptly named route, the default route is going to be to ip 0.0.0.0 and then the gateway.
Not sure if you can do this per application, but you can do it system wide!