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.
Related
I am working on an Ionic 2 application. My use case is quite simple, I display a page, and I use the angular2's Http service to retrieve a string on a remote API, and I display this string on my page. Every seconds, I did the same "GET" request, and I display this string.
If I start my application with WiFi activated and connected to a network, every things works fine. Then when I disable WiFi, I except my HTTP Packets to be sent through mobile network, but my HTTP request never get out of my phone (I check in my backends logs, and I can't see any trace of them).
Of course, this problem doesn't occurs with my development phone (on Android 5.x), but only on my customer's phone (on Android 4.4)... I guess that the old android version can't switch the network interface of an already opened TCP connection.
Does some one have already experienced this kind of problem ? And is there some solutions I can develop in my app ?
how can I execute HTTP requests or open a socket on Android Wear? I used to think that's impossible but the Web Browser for Android Wear app says the folloing:
"[..] works even when your phone is off if you have a smartwatch with Android Wear 5.1 and WiFi"*.
I tested it and that app CAN connect the internet when the paired phone is powered off.
Whenever I open a socket or try HTTP requests on Android Wear I always get a ConnectException saying failed to connect to http://foo.com (similar stack trace here). So I'm doing something different then that app is doing and I'd like to understand what that is.
Context: I'm working on proof-of-concept and just want to be able to execute HTTP requests and open sockets. I'd love to know if there's any way to do that. Even if it includes rooting the watch and doing some adb magic.
To clarify: I know about the Data Layer API and i'd still like to be able to just do HTTP requests and open sockets.
If your watch has Wifi and it is set up correctly, then you can make network calls on your watch when your watch is disconnected from the phone; when you connect to your phone via BT, wifi will be disabled. While it is enabled, you should be able to treat that as a usual network connectivity and make network calls. But keep in mind that if you write an app that relies on this, your app will fail to work when it gets connected to a phone so you need to handle that case and provide an alternative for your app to get the same data (i.e. using the phone's connectivity).
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
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 am currently working on the creation of an application for mobile devices with Android which should be capable of sending and receiving messages through local means like wifi and bluetooth. After making a research about it I found out that Android doesn't have a native wifi ad hoc functionality and that the only way to achieve that is through rooting your device and patching the wpa_suplicant and some other similiar methods which are not generic for every device.
Synopsis of what I am trying to achieve:
An application for Android that scans for devices nearby and tries to connect to them automatically. If the other device also got this application isntalled then it automatically accepts any connection request it receives and when the connection is established it sends back a message which ensures that it has this app installed so that both can start exchanging messages automatically and the users don't have to allow/deny any of the sending/receiving in real time.
My questions are:
Is there any way of sending messages through wifi between 2 android
devices without the use of internet?
Since Ad Hoc is not supported, is there any alternative?
In case the answer to 2 is no , then is it possible to make one of
the devices to be an access point so the other can connect to it and
vice versa.
As for bluetooth, is it possible to skip or automate the allow/deny
pop up of an incoming connection and the pairing procedure?
I'll be glad if you could give me an answer to those and/or any suggestions.
You'll be able to communicate between two devices over wifi without a router/access point using Wifi Direct (P2P) in Android 4.0.
Otherwise, I don't believe you can achieve this over Wifi (bluetooth would be the next best alternative). Since 2.2 you can set your phone up to be a Wifi Access Point (for tethering your device's network connection), but during this time, the Wifi hardware is unavailable for normal use. If you had 3 devices it could work (1 AP, 2 connecting to it).
Is this for a constant connection, or a one-off small delivery? You could use NFC if you want to transmit a URL for example (Android Beam integrates this type of sharing in Android 4.0, but could implement it in 2.3).