I'm developing an android app with a capability of connecting to WiFi Networks through it, and everything is OK, I'm creating available WiFi Nets list and the user user one of them to establish connectivity, the following method is the one which is used to establish that connectivity :
int netId;
boolean disableOthers;
wifiManager.enableNetwork(netId, disableOthers);
now I've Two scenarios:
The first one is to set disableOthers to true and that will disable the the connected Network in order to establish a connection with the mentioned one.
The second one is to set disableOthers to false and that will NOT disable the the connected Network and thus the new Network Connection will not be established as there's already a successfully established connection exists.
So, I don't have any options but using case one, but the problem is upon disabling the connected network, if the connection to the new Network fails due to any reason like wrong password or out of range, the device becomes without any Net connection established, So, all I want is a way enables me to regain the previous connection when the aiming to connect to a new one fails.
UPDATE:
Regarding to #bmartins answer, I did the tip the following way :
public static int net_id;
// storing the current connection Net.ID
WifiInfo info = wifiManager.getConnectionInfo();
net_id = info.getNetworkId();
// connecting to the new Net
private void connect(networkId){
wifiManager.disconnect();
wifiManager.enableNetwork(networkId, true);
wifiManager.reconnect();
wifiManager.saveConfiguration();
}
And then when the connection fails:
connect(net_id);
Sounds like a logical issue.
Why not keep track of the last successful netId? If new fails ->
wifiManager.enableNetwork(lastGoodNetId, true);
Related
The moment I get on a wifi connection, the cellular network is completely lost even though the cellular network indicator is definitely on.
This is my network request
val request = NetworkRequest.Builder().run {
addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
build()
}
connectivityManager.registerNetworkCallback(request, callback)
I've tried looking in the connectivityManager.allNetworks list and it's no where to be found. Only the wifi network is in there.
What's even weirder is there is one other cellular network that is always there. It does not have the same ID as my cellular network. There's no connection that can be made with it. It never shows up with registerNetworkCallback. The capabilities on it always include "valid" and "internet"
What am I seeing here? Why is my cellular network lost? What is this phantom cellular network?
targetSdkVersion: 29
Device: Galaxy S10 - Android 12
I figured this out.
If you call registerNetworkCallback the above will happen, but if you call requestNetwork with TRANSPORT_CELLULAR,
connectivityManager.requestNetwork(request, callback)
Android will keep the cellular network around. I was so confused because the documentation was so lacking. Once you do that, it will ask you to add the CHANGE_NETWORK_STATE permission.
After this step, the network is available, but you won't be able to make any request with it. You have to call
connectivityManager.bindProcessToNetwork(theCellularNetwork)
to get any connection.
After this is done, the cellular network can be used in tandem with the wifi network. You can even send some traffic to one and some to the other. If you use OkHttp like I do, you just bind the client with the network's socketFactory
val client = OkHttpClient().newBuilder().run {
socketFactory(network.socketFactory)
build()
}
client.newCall(
Request.Builder().url("https://example.com").build()
).execute().let {
Log.i(TAG, "Fetched ${it.body!!.string()}")
}
The cellular network isn't lost, but your app isn't allowed to use it. Once WiFi is connected, everything is forced to use that connection. The only exception to this rule is if your phone has a feature called "Dual Acceleration", which allows the cellular connection to stay active (and obviously, the user would have to enable that feature). Alternatively, you may have a setting in your phone's Developer Options called "Cellular Data Always Active", which will do the same thing.
But needless to say, you can't rely on either of those 2 features being enabled in a production environment. So, just assume that when WiFi is connected, that's the only connection that your app can use
My app needs to use a wifi network (without internet, thus android avoids it by default). And I want it to be simple to use (like Grandma doesn't have to be manually changing settings). This is just an personal IoT device so I want to use ConnectivityManager.BindProcessToNetwork(Android.Net.Network network). How do I get the Android.Net.Network associated with the currently connected wifi network so I can use BindProcessToNetwork?
//c#(Xamarin)
//my rudimentary attempt to get the connected wifi network:
var networks = ConnectivityManager.GetAllNetworks();
foreach (Network network in networks) {
NetworkCapabilities networkCability =ConnectivityManager.GetNetworkCapabilities(network);
if (networkCability.HasTransport(TransportType.Wifi))
{
currentWifiNetwork = network; // this is never reached
}
}
ConnectivityManager.BindProcessToNetwork( currentWifiNetwork );
Is there not a distinct Network object for all the phones currently in use WiFi, cellular, etc... networks?
This blog got me close: https://android-developers.googleblog.com/2016/07/connecting-your-app-to-wi-fi-device.html.
A binding socket method would work too (except the wifi network isn't an available network until the use network without internet box is checked). I just need to the App to use urls that are on port 8080 via the wifi.
I want to avoid having to manually telling Android to "use the network with no internet".
Cheers
Update
When I run this, there are only two Networks returned by ConnectivityManager.GetAllNetworks(), and looking at them in the debugger, one is the Cellular network with internet and mms, and the other is another Cellular network without internet and mms. So no ConnectivityManager.GetAllNetworks() doesn't get the wifi network as it appears android won't even add the wifi network unless it has internet! If the phones data is disabled Android will switch and use the internet-less wifi for all traffic (without having to check use the network anyways box).So their must be a way to get the WiFi network bound to the app! or...
How does one programmatically check the use network anyways box!?
I have not seen a solution to this. Just a bunch of open questions all over the web. At this rate I might just use dnsmasq on the iot device and a spoofing web server to make android think it has internet.
I also see that API 29 has NetworkBuilder and that you can specify a request for a WiFi network without internet capabilities...but I need lower API support.
Update:
Android now supports requesting local only wifi networks (networks without the NetworkCapabilities#NET_CAPABILITY_INTERNET capability) check out :
https://developer.android.com/reference/android/net/wifi/WifiNetworkSpecifier.Builder
Original Answer:
This is the solution that I came up with (for Api 28 ). It prioritizes WiFi over 4G/5G (Data) regardless of internet capability via a NetworkRequest, and then allows for both internet over 4G/5G (Data) and the app to use its local WiFi services:
public static WifiManager WifiManager { get; } = (WifiManager)Android.App.Application.Context.GetSystemService(Context.WifiService);
public static ConnectivityManager ConnectivityManager { get; set; } = (ConnectivityManager)Android.App.Application.Context.GetSystemService(Context.ConnectivityService);
public bool ConnectToWifi(string ssid, string password, bool previouslyConnected = true)
{
if (!WifiManager.IsWifiEnabled)
WifiManager.SetWifiEnabled(true); //turn on wifi if not on
var formattedSsid = $"\"{ssid}\"";
var formattedPassword = $"\"{password}\"";
var wifiConfig = new WifiConfiguration
{
Ssid = formattedSsid,
PreSharedKey = formattedPassword,
Priority = 0
};
_NetworkId = WifiManager.AddNetwork(wifiConfig);
WifiManager.Disconnect();
bool enableNetwork = WifiManager.EnableNetwork(_NetworkId, true);
NetworkRequest.Builder builder = new NetworkRequest.Builder(); //request that WiFi be prioritized over the 4G internet capable network.
builder.AddTransportType(TransportType.Wifi);
ConnectivityManager.RequestNetwork(builder.Build(), new BindNetworkCallBack ());
return enableNetwork;
}
This call back then binds the appropriate WIFI network to the app(process)! Allowing for the user to both simultaneously use the app with the local server over WIFI, and other apps that still require internet, as Android OS can allow other processes to access the internet via the 4G/5G data connection!
public class BindNetworkCallBack : ConnectivityManager.NetworkCallback
{
public override void OnAvailable(Network network)
{
if (WifiManager.ConnectionInfo.BSSID == NetworkBSSID) /*
The only way on Android (API 28+) to check if the acquired network is
the one you want is to use the BSSID (MAC address) of the network.
You can omit the if statement if you want to presume the acquired network is correct/
cannot know the MAC address...
*/
{
try
{
ConnectivityManager.BindProcessToNetwork(network);
}
catch (Exception ex)
{
Debug.WriteLine(#"\tERROR Unable to Bind process to network {0}", ex.Message);
}
}
}
}
"bindProcessToNetwork Binds the current process to network. All Sockets created in the future (and not explicitly bound via a bound SocketFactory from Network.getSocketFactory()) will be bound to network. All host name resolutions will be limited to network as well." - Android Docs
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 am creating an app, which can list out all the available wifi's in a ListView. If I select one of the wifi's in the list, which was cached before in List<WifiConfiguration> list = wifiManager.getConfiguredNetworks(); then it should connect to it. If the WifiConfiguration list doesn't contain the selected wifi, then nothing happens. My problem is, that sometimes I select a wifi from the list (which I know for sure is in the WifiConfiguration list), but it doesn't connects to it. Instead it connects back to the previously connected wifi. After some attempts (selecting again and again the same wifi) it connects to it finally. This doesn't happen always, just sometimes. What can be the problem? Here is my code snippet:
// Go through all the cached wifis and check if the selected GoPro was cached before
for (WifiConfiguration config : configurations) {
// If it was cached connect to it and that's all
if (config.SSID != null && config.SSID.equals("\"" + mDrawerListView.getAdapter().getItem(position) + "\"")) {
// Log
Log.i("onReceive", "Connecting to: " + config.SSID);
mWifiManager.disconnect();
mWifiManager.enableNetwork(config.networkId, true);
mWifiManager.reconnect();
break;
}
}
This is what's going on. Basically, you can tell the OS to disable a network, and you can tell the OS to enable a network, but it's not possible to tell the OS what network to connect to.
If there are multiple WiFi access points in range that are both configured on the device (and both are in the enabled state), the OS will decide which one to connect to.
The only way to force the OS to connect to one of the networks in range instead of the other one is to call disableNetwork() on the network that is in range that you don't want to connect to.
Let's go through your code line by line:
mWifiManager.disconnect();
The line above tells the OS to disconnect from the currently connected WiFi access point.
mWifiManager.enableNetwork(config.networkId, true);
The line above tells the device to set the network to the enabled state if it was previously in the disabled state.
mWifiManager.reconnect();
From the documentation:
Reconnect to the currently active access point, if we are currently
disconnected. This may result in the asynchronous delivery of state
change events.
So, when you say Instead it connects back to the previously connected wifi., it's working exactly as expected, as the OS is re-connecting to what it considers the currently active access point.
If you really want to disable the other network so that the OS will connect to the one you just clicked on, you could do something like this:
// Go through all the cached wifis and check if the selected GoPro was cached before
WifiInfo info = mWifiManager.getConnectionInfo(); //get WifiInfo
int id = info.getNetworkId(); //get id of currently connected network
for (WifiConfiguration config : configurations) {
// If it was cached connect to it and that's all
if (config.SSID != null && config.SSID.equals("\"" + mDrawerListView.getAdapter().getItem(position) + "\"")) {
// Log
Log.i("onReceive", "Connecting to: " + config.SSID);
mWifiManager.disconnect();
mWifiManager.disableNetwork(id); //disable current network
mWifiManager.enableNetwork(config.networkId, true);
mWifiManager.reconnect();
break;
}
}
Hi instead of disabling the previous network , you can change the priority of the network you to connect to greater than all other configured networks and then when you reconnect() . it will connect to the highest priority network in range.
wificonfig.priority = 10000;
wifiManager.updateNetwork(wificonfig);
wifiManager.saveConfiguration();
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, false);
wifiManager.reconnect();
I'm working on a project which need to connect to a Wi-Fi in a background service, the service running when the device is screen off.
The connecting code is like below:
public boolean connect_android(String ssid) {
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
boolean find = false;
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
find = true;
break;
}
}
while after the connecting code is executed in the background running service, the device never connect to the Wi-Fi successfully until the screen is turned on. I logged the supplicant state, it is in complete state. As the google docs says:
This state indicates that the supplicant has completed its processing for the association phase and that data connection is fully configured. Note, however, that there may not be any IP address associated with the connection yet. Typically, a DHCP request needs to be sent at this point to obtain an address.
So can i come to the conclusion that, when the screen is turned on, a DHCP request is send the device receives an IP and the connection is complete successfully. But who is in charge of sending the DHCP, the wpa_supplicant or Android framework, is there any docs about this? How can i connect to a Wi-Fi AP without turn on the screen? Thanks in advance!
Maybe because your device use feature turn of wifi when screen of (Settings -> Wireless and network -> WiFi settings -> (menu button) Advanced -> Wifi sleep policy)
Your app will cannot connect to network