I am trying to establish tcp socket connection when my app is in background, before I start connect to server, I checked the network state with the code below
ConnectivityManager connectivityManager = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
NetworkInfo.DetailedState state = info.getDetailedState();
and find the state is BLOCKED, which will cause failure for the tcp connection.
I was wondering is there any method to change network state from Blocked to Connected state? Any advice is appreciated, thanks in advance.
PS: I am working on MIUI rom, other roms may not have this issue.
You can't simply force a change of state. This state indicates that the device is simply not allowed to use the network, even though it might be connected. The BLOCKED state typically means that the user of the device has chosen to not allow background data access. (Maybe they have limited data on their plan and need to control access to data usage.)
Related
I wrote an app that is triggering a Sony qx smartphone attachable camera over wifi. However I need to transfer the images off the phone over another local network in real time. Since the wifi card is being used for qx connection I need to be able to use ethernet over usb for transferring images off the phone. Http requests will be used to trigger the camera and send the images off the phone.
Is it possible in one android app on a phone with two network interfaces setup to specify for certain http requests to use one network interface and for others to use another network interface ? Does this need to be done through routing tables, not java?
The phone I'm using is a rooted nexus 6p.
Update:
Currently, I was able to get an Ethernet adapter working with the device (Nexus 6P). The device is connected to a local network over Ethernet. When the Wi-Fi interface is off, I can ping all devices on the local network the device is connected to over Ethernet. However, I am unable to access the web servers (Not using DNS) of any of the devices on that network (which I know they are running), i.e. Http via a browser app. The nexus 6p is connected to the network over Ethernet via a Ubiquiti Station. This seems to be a routing issue.
I can tether(usb interface) and use Wi-Fi in one app, so that leads me to believe it is possible to use Ethernet and Wi-Fi.
Update2:
After more testing, it seems to be that it is a permissions issue. Since when I ping the network the device is connected to over Ethernet without first running su in the terminal the network doesn't exist. However, when I run su then ping, I can ping the network. Thus it seems my app needs to get superuser permission before accessing Ethernet. I've granted it superuser access, but nothing has changed. I read that simply running su isn't enough from one of the comments in this post. This is because su just spawns a root shell that dies. This also explains why I couldn't access any of the web servers on this network via a browser app. Is it possible to grant my app access to the Ethernet interface when making HTTP calls like give HttpURLConnection root access, if that makes any sense (running su doesn't work)? There seems to definitely be a solution since HttpURLConnection can make calls over the USB tethering interface (Nexus 6P calls it rndis0) fine.
Update 3:
I found online here , that I can make my app a System app (thought this might grant the app eth0 access). I just moved my app to /system/app and then rebooted. However, this didn't seem to give the app anymore privileges (thus not solving the problem) , or there is something else required to make the app system than just copying it to /system/app.
Update 4:
So I was able to get Ethernet working on every app without root permissions! It seemed to be that it only works over DHCP and does not like static connections, which I was using. It works with Wi-Fi enabled, however, I cannot contact any of the devices on the Wi-Fi network when Ethernet is enabled. Is there a way around this? Does it have to do with setting two default gateways?
Since you were programming in Nexus 6P, you can try to use the new API added in ConnectivityManager to select the ethernet as your preferred network connection for your process.
Since I can't build the similar environment like yours, I am not sure if it works. It's just a suggested solution, totally not tested and verified.
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
Network etherNetwork = null;
for (Network network : connectivityManager.getAllNetworks()) {
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
etherNetwork = network;
}
}
Network boundNetwork = connectivityManager.getBoundNetworkForProcess();
if (boundNetwork != null) {
NetworkInfo boundNetworkInfo = connectivityManager.getNetworkInfo(boundNetwork);
if (boundNetworkInfo.getType() != ConnectivityManager.TYPE_ETHERNET) {
if (etherNetwork != null) {
connectivityManager.bindProcessToNetwork(etherNetwork);
}
}
}
Just to give a little more explanation on how this finally got solved.
Utilizing #alijandro's answer I was able to switch back and forth between Ethernet and Wi-Fi in one app. For some reason for the Ethernet to work it required the network gateway to supply DHCP address, not static. Then since the bindProcessToNetwork, used in #alijandro's answer is per-process, I decided to split communications with the QX camera into a Service that runs in a separate Process. The main Application (another process) would post images over Ethernet to a local network. I was successfully able to contact the devices on the local network via HTTP over Ethernet while simultaneously triggering the QX over Wi-Fi. Currently, I used Messenger to communicate using IPC to tell the QX triggering Service what methods to call.
Most of android tv boxes can use wifi and ethernet together. In my device, i can enable ethernet from this path ---
Settings -> More ... > Ethernet ---
But your device wont have a menu like that as i understand. So you should make an app to do that. This application needs to access some system specific resources so your device needs to be rooted or application needs to signed with system signature.
Also this topic can help you link
There is an easy way to do this that will answer the OP's original question about how to do this with a single application (not two separate app processes) using ConnectivityManager.requestNetwork().
The docs for ConnectivityManager.requestNetwork() allude to this:
... For example, an application could use this method to obtain a
connected cellular network even if the device currently has a data
connection over Ethernet. This may cause the cellular radio to consume
additional power. Or, an application could inform the system that it
wants a network supporting sending MMSes and have the system let it
know about the currently best MMS-supporting network through the
provided NetworkCallback. ...
For OP's scenario of using Wi-Fi for some traffic and ethernet for other traffic one only needs to call ConnectivityManager.requestNetwork() twice with two separate requests. One for TRANSPORT_WIFI and one for TRANSPORT_ETHERNET. The operative item here is we need a way to uniquely identify these networks. For OP's scenario, we can use transport type.
final NetworkRequest requestForWifi =
new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build();
final NetworkRequest requestForEthernet =
new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
.build();
final ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkCallback networkCallbackWifi = new NetworkCallback() {
#Override
void onAvailable(Network network) {
// Triggers when this network is available so you can bind to it.
}
#Override
void onLost(Network network) {
// Triggers when this network is lost.
}
};
final NetworkCallback networkCallbackEthernet = new NetworkCallback() {
#Override
void onAvailable(Network network) {
// Triggers when this network is available so you can bind to it.
}
#Override
void onLost(Network network) {
// Triggers when this network is lost.
}
};
connectivityManager.requestNetwork(requestForWifi, networkCallbackWifi);
connectivityManager.requestNetwork(requestForEthernet, networkCallbackEthernet);
Then, once the callbacks trigger, you can then in the pertinent code (e.g. OP's code for transferring images), listen for onAvailable(Network network) and use the provided Network with Network.OpenConnection() to connect to an HTTP server using that network.
This would allow you to connect to two separate Networks from the same application.
I want my app to automatically notify the user when the networkType changes(eg from EDGE to 3G or 3G to 1XRTT etc).
Using the getNetworkType() method, I have to continuously check for a change in networkType. Is there some kind of intent being broadcasted throughout the system when a change is detected.
Look at the ConnectivityManger.CONNECTIVITY_ACTION Ref
From Documentation
A change in network connectivity has occurred. A default connection has either been established or lost. The NetworkInfo for the affected network is sent as an extra; it should be consulted to see what kind of connectivity event occurred.
While working on a little project that integrates with GCM, I've stumbled across a bit of a strange issue.
Some times when I start watching the log to see if messages are received, messages do not appear to be coming through until I have changed the network state (I.E. originally on WiFi, if I turn off WiFi and move to Mobile Data, the messages arrive fine). After I've changed the network state, messages start to arrive perfectly fine, and the same applies once I change the network state back to what it was before (in this case, WiFi) the messages continue to be received.
The project itself includes the ability to start on boot (starts the GCMBaseIntentService on boot), which again works perfectly fine, and I'm sure the app / service is running as I've manually started up the app when this issue occurs (which also checks to see if the service is running, and if it's not it runs it and checks to see if it's registered).
Has anyone else come across this issue, or has any pointers as to how I could resolve this? I'm not seeing anything of much help in the log between the time messages are not being received and when they are (after changing the network state). I've gone through the GCM docs and can't see any mention of messages not being received due to a time-out (on the device itself), or any config options that might affect this.
Appreciate any assistance - I can provide source if needs be, although it hardly deviates from the demo app provided in the android-sdk.
I've noticed this as well. Although I haven't dug into the actual code, here's my understanding of why this happens.
GCM (and most push messaging services) works by keeping a long-lived socket open to Google's push notification server. The socket is kept open by sending "heartbeat" messages between the phone and server.
Occasionally, the network state may change and this socket will be broken (because the IP address of the device changes, from 3g to wifi, for example). If the message comes in before the socket is reestablished, then the device will not immediately get the message.
The reconnection only happens when the phone notices the socket is broken, which only happens when it tries to send a heartbeat message.
Again, just my basic understanding of how it works and why it happens, and I could be wrong.
There are many causes for GCM message delays. If message start to arrive after you changed network state, or switched on/off airplane mode - the most likely cause is a network that closes the connection without sending FIN/RST.
GCM maintains a long-lived connection - and reconnects if it knows the connection was broken.
A router/AP/NAT is supposed to send a FIN or RST to terminate the TCP connection - so GCM and servers will know the connection is dead.
However a number of routers and mobile operators don't do this, and then GCM needs to rely on the heartbeat, ~15 min on Wifi, more on mobile. There is a trade-off between battery life/network use and heartbeat frequency.
As per your comment in above answer and according to my experience with GCM push notifications there isn't any reason that if network(internet connection) is available you should not receive push notifications I always check internet connection availability before running the application for push notification like this try checking this if this is true you should receive push notifications
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
so, If #theelfismike thinking is true, may I use something like:
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
// here the network changed to Wifi so I can send a heartbeat to GCM to keep connection
if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
//here the network changed to mobileData so I can send a heartbeat to GCM to keep connection
}
is my solution good?
in the GCMIntentSevice class ,when you receive message from server ,onMessage method gets called so at that time you can do something like...
PowerManager pm = (PowerManager) getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(
(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP),"TAG");
wakeLock.acquire();
and you have to add
<uses-permission android:name="android.permission.WAKE_LOCK" />
permission in you manifest file..
this should do the trick...
I have an android app that tried to connect to a website in an AsyncTask and perform some tasks. My application always seems to crash when there is a change in the network connection i.e The app is connected to a wifi network initially, but loses connection and switches to mobile network. When this happens my Android Application crashes. My code is surrounded within a try catch block, so I'm not sure why the application would crash ? How do I fix my problem ?
Thanks!
WifiInfo info = WifiManager.getConnectionInfo();
if (info.getSSID() != null) {
String ssid = info.getSSID();
...
}
I don't know if it would be overkill to write:
if (info != null && info.getSSID() != null)
Also, the BroadcastReceiver that I need to monitor when a connection is made is WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION. It turns out that I had a copy and paste error in my onResume(), and I wasn't really registering WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION.
or u can use following link
Android: How to Enable/Disable Wifi or Internet Connection Programmatically
You have to make sure you wait for a network connectivity to be available before continuing your websites connections.
Plus, be aware that if you try to enable a mobile connection without disabling WiFi beforehand, the Android system will automatically shutdown the newly enabled mobile connection (it gives priority to WiFi to sum up). So you need to make sure you do it in the right order :
(WiFi enabled)
(WiFi ready)
Websites connections STARTED
Websites connections STOPPPED
(WiFi disabled)
(WiFi unavailable)
(Mobile enabled)
(Mobile ready)
Websites connections RESUMED
I want to write automated tests to run on the emulator that verify that my app behaves correctly when the network connection is spontaneously dropped. To do so, I'd like to write tests like the following:
Enable network connection.
Start some app network operation such as a long file download.
Disable network connection.
Verify that lost network connection dialog is shown.
I have seen various stackoverflow answers that address this by suggesting manually disabling the network connection from the host PC via the command line. This cannot be not automated by a test that is running on the emulator, because the emulator has no access to its host PC. And running the commands in a host PC shell script would be very difficult to attempt to automatically synchronize with an emulator test.
Alternately I've seen answers that suggest manipulating the emulator's network settings, which apparently don't actually disable access, they only cause explicit connectivity checks like networkInfo.isConnected() to report a disconnected state. They still allow bidirectional communication. (E.g., when emulator network access is disabled, the browser is still fully functional.)
Has anyone been able to construct automated tests that run on the emulator and actually turn on and off network access as needed mid-test?
There is presently no way to truly disable network access programmatically from within a unit test. I filed an enhancement request for this issue.
If you're not averse to invoking an undocumented method, you can do the following to turn off / on the data connection.
public void enableData(Context context, boolean enable) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
Method m = cm.getClass().getDeclaredMethod("setMobileDataEnabled", boolean.class);
m.invoke(cm, enable);
} catch (Exception e) {
}
}