I have to use WiFi and lte at the same time on Android.
Using ConnectivityManager, I was able to send a request via lte while wifi was active
However, I never used the two at the same time.
I currently use these methods.
private static ConnectivityManager cm;
private static NetworkRequest.Builder req;
cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
req = new NetworkRequest.Builder();
req.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
req.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
cm.requestNetwork(req.build(), new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
//here you can use bindProcessToNetwork
cm.bindProcessToNetwork(network);
}
});
Is there a way I can request wifi and lte at the same time?
You can use onAvailable() of NetworkCallback to check for network changes.
In addition, bindProcessToNetwork() declares that only certain networks will be used.
Use ConnectivityManager.getAllNetworks() with the my simple example below.
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
Network []networks = connectivityManager.getAllNetworks();
for(int i = 0 ; i < networks.length ; i++) {
NetworkInfo info = connectivityManager.getNetworkInfo(networks[i]);
if(info.getTypeName().equals("MOBILE")) {
// This is mobile network (3G/LTE)
// Use networks[i].bindSocket(java.net.Socket) for low-level socket communication
// Use networks[i].openConnection(java.net.URL) for HTTP request
}
else if(info.getTypeName().equals("WIFI")) {
// This is WiFi network
// Use networks[i].bindSocket(java.net.Socket) for low-level socket communication
// Use networks[i].openConnection(java.net.URL) for HTTP request
}
else {
// This is another network (WIFI_P2P, Ethernet, Bluetooth, ...)
}
}
Related
I have the following code that sets the default network of my internet connection.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
NetworkCallback networkCallback = new NetworkCallback() {
#Override
public void onAvailable(Network network) {
boolean result = ConnectivityManager.setProcessDefaultNetwork(network);
Log.d(TAG, "success? " + result);
}
};
NetworkRequest networkRequest = builder.build();
connMgr.requestNetwork(networkRequest, networkCallback);
connMgr.registerNetworkCallback(networkRequest, networkCallback);
}
This works properly. However, later on, when I check using the following code:
ConnectivityManager connMgr = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfoMobile =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
networkInfoMobile.isConnected(); // returns false!
This always returns false. What am I doing wrong?
What I did is I saved the network provided by onAvailable() callback. Then later, I checked the connectivity thru:
NetworkInfo networkInfo = connMgr.getNetworkInfo(networkFromOnAvailable);
networkInfo.isConnected();
Cheers!
I have both WIFI and mobile data enabled. In wifi I have connected to an OBD which has no internet connection but will stream some data if connected. In order to start that stream I used the below code:
ConnectivityManager connectivityManager = (ConnectivityManager) getCurrentActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
if (SDK_INT >= LOLLIPOP) {
NetworkRequest.Builder request = new NetworkRequest.Builder();
request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
connectivityManager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
if (SDK_INT >= LOLLIPOP && SDK_INT < M) {
ConnectivityManager.setProcessDefaultNetwork(network);
} else if (SDK_INT >= M) {
connectivityManager.bindProcessToNetwork(network);
}
}
});
}
I got the code from this answer.
How to stay connected through mobile network after WIFI is connected on Android?
When it comes to get the active network into at connectivityManager.getActiveNetworkInfo() I get the network type as MOBILE[LTE]. Once it hits the onAvailable method, whenever I try to get the active network type it says WIFI[].
After this the stream gets started. Now I want the mobile data to be activated again since it is enabled. So I tried the same code after the stream started by just replacing TRANSPORT_WIFI with TRANSPORT_CELLULAR in request.addTransportType.
ConnectivityManager connectivityManager = (ConnectivityManager) getCurrentActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
if (SDK_INT >= LOLLIPOP) {
NetworkRequest.Builder request = new NetworkRequest.Builder();
request.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
connectivityManager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
if (SDK_INT >= LOLLIPOP && SDK_INT < M) {
ConnectivityManager.setProcessDefaultNetwork(network);
} else if (SDK_INT >= M) {
connectivityManager.bindProcessToNetwork(network);
}
}
});
}
What happens is, when I debug this process, NetworkInfo always has the WIFI connection even after it hits the onAvailable method.
Please pinpoint if I was wrong somewhere or am I missing something.
Both wifi and data connection are enabled.
Since I need to use mobile data to send a http request to mobile carrier to get phone number, But android will use wifi as prior, so How can I use data connection instead of WIFI?
When I enable the wifi and mobile data int the device. I use getAllNetworks() method, but it always returns wifi. I don't know Why getAllNetworks just return wifi when I enable both wifi and mobile data?
When I just enable the mobile data, the getAllNetworks() return mobile data info.
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] network = connectivityManager.getAllNetworks();
if(network != null && network.length >0 ){
for(int i = 0 ; i < network.length ; i++){
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network[i]);
int networkType = networkInfo.getType();
if(ConnectivityManager.TYPE_MOBILE == networkType ){
connectivityManager.bindProcessToNetwork(network[i]);
}
}
}
Does some one know how to use data connection instead of WIFI when both wifi and data connection are enabled?
You can use data connection instead of WIFI only if you are working on Android Lollipop.
And it seems you are trying to use Android Lollipop with target API 23 because you used bindProcessToNetwork instead of setProcessDefaultNetwork.
Android Lollipop allows the multi-network connection.
ConnectivityManager cm;
cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder req = new NetworkRequest.Builder();
req.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
req.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
cm.requestNetwork(req.build(), new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
//here you can use bindProcessToNetwork
}
});
I am working on an app that saves your credentials and logs in to the university wifi automatically when connected to a particular ssid. Everything is working fine. But when mobile data is on and wifi gets connected, the login request is sent through the mobile data (since the wifi connection is not active).
I read on the documentation and tried this.
if(Build.VERSION.SDK_INT >= 21){
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder();
requestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
connectivityManager.registerNetworkCallback(requestBuilder.build(), new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
ConnectivityManager.setProcessDefaultNetwork(network);
}
});
}
new LoginTask().execute();
This doesn't solve my problem.
Any suggestions?
I solved this problem myself.
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] networks = connectivityManager.getAllNetworks();
NetworkInfo networkInfo;
Network network;
for (int i = 0; i < networks.length; i++){
network = networks[i];
networkInfo = connectivityManager.getNetworkInfo(network);
if ((networkInfo.getType() == ConnectivityManager.TYPE_WIFI) && (networkInfo.getState().equals(NetworkInfo.State.CONNECTED))) {
ConnectivityManager.setProcessDefaultNetwork(network);
break;
}
}
I need to connect my mobile by wifi to a connected device. This hot spot have not internet.
Before 5.0 I did like this to check if I was connected to the wifi
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(mWifi.isConnected()&& wifiManager.getConnectionInfo().getNetworkId()==addedNetwork)
But now with the 5.0 version of android, when I check isConnected it always returns false because of the none connectivity to internet.
How can I manage this case ?
Regards
I haven't tried this out but as per the google docs this should work
ConnectivityManager cManager = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new Builder();
//Indicates that this network should be able to reach the internet.
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
NetworkRequest build = builder.build();
cManager.requestNetwork(build, new NetworkCallback() {
#Override
public void onAvailable(Network network) {
super.onAvailable(network);
// do what you want to do here
}
});`