I'm working on android 9 and I want to give preference to WiFi over Ethernet.
I tried giving wifi higher priority than ethernet in my config.xml file as shown below but still my ethernet has higher priorty.
<string-array translatable="false" name="networkAttributes">
<item>"wifi,1,1,2,6000,true"</item>
<item>"ethernet,9,9,0,6000,true"</item>
</string-array>
I searched online and I found that default network preference can be given in ConnectivityManager.java. But it shows deprecated in API28.
#Deprecated
public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
Also, getNetworkInfo and startUsingNetworkFeature is deprecated as well.
#Deprecated
#RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public NetworkInfo getNetworkInfo(int networkType) {
#Deprecated
public int startUsingNetworkFeature(int networkType, String feature) {
How do i give WIFI higher priority than ethernet on android 9?
You have two options on how you can handle this.
Option 1: Update AOSP source code
In Android 9, ConnectivityService will use a static network score to determine which priority to use various network transport types (ethernet vs Wi-Fi vs cellular, etc).
Ethernet has a network score of 70 (link) while Wi-Fi has a network score of 60 (link).
In your case, if I wanted to prioritize Wi-Fi over Ethernet, you can change the score to reflect the new priority (e.g. change Wi-Fi to 71 in the Wi-Fi link above or similarly, lower ethernet's score to say 59 in its factory).
Example with Wi-Fi:
private static final int SCORE_FILTER = 71;
Option 2: Use Resource Overlay
There is a resource overlay that can be used to manually configure the network capabilities on Ethernet networks named config_ethernet_interfaces (link).
<!-- Configuration of Ethernet interfaces in the following format:
<interface name|mac address>;[Network Capabilities];[IP config];[Override Transport]
Where
[Network Capabilities] Optional. A comma seprated list of network capabilities.
Values must be from NetworkCapabilities#NET_CAPABILITY_* constants.
The NOT_ROAMING, NOT_CONGESTED and NOT_SUSPENDED capabilities are always
added automatically because this configuration provides no way to update
them dynamically.
[IP config] Optional. If empty or not specified - DHCP will be used, otherwise
use the following format to specify static IP configuration:
ip=<ip-address/mask> gateway=<ip-address> dns=<comma-sep-ip-addresses>
domains=<comma-sep-domains>
[Override Transport] Optional. An override network transport type to allow
the propagation of an interface type on the other end of a local Ethernet
interface. Value must be from NetworkCapabilities#TRANSPORT_* constants. If
left out, this will default to TRANSPORT_ETHERNET.
-->
<string-array translatable="false" name="config_ethernet_interfaces">
<!--
<item>eth1;12,13,14,15;ip=192.168.0.10/24 gateway=192.168.0.1 dns=4.4.4.4,8.8.8.8</item>
<item>eth2;;ip=192.168.0.11/24</item>
<item>eth3;12,13,14,15;ip=192.168.0.12/24;1</item>
-->
</string-array>
This is indexed on ethernet interface name so you'll need to have the same ethernet name in all cases, e.g. eth0 for most people. You could update the above config to play around with the capabilities. In your case, you could just omit the NOT_RESTRICTED capability (link) in which case Ethernet would never be used as the default network only leaving Wi-Fi to be prioritized higher.
<!-- Restricting eth0 -->
<string-array translatable="false" name="config_ethernet_interfaces">
<item>eth0;11,12,14;;</item>
</string-array>
In fact, the Android Cuttlefish target (link) does this today for similar reasons. Note, the above config would mark eth0 as restricted so it probably wouldn't get an IP or be used at all (except by potentially apps with restricted network access). You can always play around with these capabilities if you want a different behavior.
Related
as far as I understood Android uses the NetworkScore class to select between available networks. Every available network is assigned with various flags and the Connectivity module in AOSP uses the policy logic for network selection found in the NetworkRanker class to determine from those flags, which network to choose.
I also got that it is not possible to directly select the network operator from API (for security reasons).
Would it be possible to change those flags programmatically (after set by NetworkScore) to "force" a certain network to be selected by the system?
Would it be possible to change the policy logic in the NetworkRanker class to block Network operators of a certain country?
I ran into an issue where my Android app cannot make outgoing connections using the TcpClient class (question located here). While debugging and conducting further research, I found that my Android device (API level 22) is receiving a subnet mask of 0.0.0.0 when it connects to my WiFi network. My DHCP server is configured to use 255.255.255.0, so I believe this discrepancy could be the root of my other problem. This is my code:
//Connect to the WiFi network
int id = WiFiManager.AddNetwork(new WifiConfiguration()
{
Ssid = $"\"{ssid}\"",
PreSharedKey = $"\"{password}\""
});
WiFiManager.EnableNetwork(id, true);
WiFiManager.Reconnect();
//Retrieve subnet mask (for debugging)
int subnet_mask = WiFiManager.DhcpInfo.Netmask;
subnet_mask returns 0, and the formatted version of the DhcpInfo class shows:
{ipaddr 10.0.0.15 gateway 10.0.0.0 netmask 0.0.0.0 dns1 10.0.0.0 dns2 0.0.0.0 DHCP server
10.0.0.0 lease 43200 seconds}
With this being the situation, is there any way I can manually change the subnet mask (or "netmask") within the Android API? I have tried using a static IP configuration as shown here, but use of those settings was deprecated in API level 17. I have also tried setting the DhcpInfo.Netmask property manually, but it takes an int. I calculated one based on this answer, but it was too large and became a uint. Lastly, I looked through a variety of classes, including Android.Net.Wifi.WifiManager, to see if there was a way to change the netmask. I didn't find anything, but I might have just been looking in the wrong places.
Any help is greatly appreciated!
Update:
For security reasons, it seems like this is not possible with the standard Android API. However, is the Android NDK able to change the subnet mask? I understand it provides lower-level access to the device, and I do not need to put this app on the app store (it is for my use only).
For security reasons, Android OS may not allow you to do this.
I know as a user I would not want an app to change my network configuration.
It seems this may have been able to be done in the past: https://stackoverflow.com/a/7142316/2913599
but that API is obsolete/deprecated in API level 17: https://developer.android.com/reference/android/provider/Settings.System.html#WIFI_STATIC_DNS1
Docs say to use WifiManager instead, but that API does not allow changing the ip, dns, gateway, again for security reasons.
If you need to get these settings changed, perhaps you can provide an instruction page for the user so they can change them themselves.
I need to connect 20+ android devices in a client-server network. Each client Android device will be communicating with the server Android device and vice versa. The client devices do not need to communicate with each other.
The server device would need access to internet for a brief period while connected to the clients.
My question is, can Wi-Fi P2P support that many connections reliably? And if yes, how do I go about implementing them?
Or will I have to ensure that all devices are on the same WLAN?
From experience, in a real-world deployment of an Android Wi-Fi Direct application, 20 devices should not be an issue.
Theoretically, the maximum number of devices in a Wi-Fi P2P group, where the GO is an Android device, is 254. The group owner is assigned the IP, 192.168.49.1. Clients are assigned an IP from the range, 192.168.49.2 to 192.168.49.254.
The group owner address is defined by the following in WifiP2pServiceImpl.java:
/* Is chosen as a unique address to avoid conflict with
the ranges defined in Tethering.java */
private static final String SERVER_ADDRESS = "192.168.49.1";
Determining the range for the clients is done as follows:
In WifiP2pServiceImpl.java, the startDhcpServer(String intf) method will start the DHCP server for a given interface - not a surprise. This method is called when the group has started and the device is the group owner.
Taking a closer look at this code, we can see that on the InterfaceConfiguration object, the link address is set to 192.168.49.1 and the prefix length is 24 (prefix length is the number of bits set in a subnet mask, here equivalent to 255.255.255.0) - this implies the answer, but we can dig a little further.
ifcg = mNwService.getInterfaceConfig(intf);
ifcg.setLinkAddress(new LinkAddress(NetworkUtils.numericToInetAddress(
SERVER_ADDRESS), 24));
ifcg.setInterfaceUp();
mNwService.setInterfaceConfig(intf, ifcg);
Next, the following commands will restart tethering with the DHCP range specified by the String[], tetheringDhcpRanges. The calls of mNwService (Network Management Service) methods will execute the appropriate netd commands.
ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(
Context.CONNECTIVITY_SERVICE);
String[] tetheringDhcpRanges = cm.getTetheredDhcpRanges();
if (mNwService.isTetheringStarted()) {
if (DBG) logd("Stop existing tethering and restart it");
mNwService.stopTethering();
}
mNwService.tetherInterface(intf);
mNwService.startTethering(tetheringDhcpRanges);
And cm.getTetheredDhcpRanges() is ultimately a reference to the following (ConnectivityManager.getTetheredDhcpRanges() -> ConnectivityService.getTetheredDhcpRanges() -> Tethering.getTetheredDhcpRanges()):
// USB is 192.168.42.1 and 255.255.255.0
// Wifi is 192.168.43.1 and 255.255.255.0
// BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
// with 255.255.255.0
// P2P is 192.168.49.1 and 255.255.255.0
private String[] mDhcpRange;
private static final String[] DHCP_DEFAULT_RANGE = {
"192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
"192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
"192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
"192.168.48.2", "192.168.48.254", "192.168.49.2", "192.168.49.254",
}
and:
mDhcpRange = context.getResources().getStringArray(
com.android.internal.R.array.config_tether_dhcp_range);
if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) {
mDhcpRange = DHCP_DEFAULT_RANGE;
}
in com.android.server.connectivity.Tethering.
Of course, it is possible for the device manufacturer to change this code, so this is also worth considering.
For those planning to deploy applications where there will be many users, a mechanism to allow a more than one device to be GO is required. If data needs to be synchronised between devices, it is simple to simulate "churn" and have GOs only be a GO for a time period before becoming a client to another GO and synchronising any data.
The max number as far as I know is not specified, so you would need to test that out to be certain. Also there could be differences between hardware.
Anyway, the basic implementation would be rather simple. The server would call GreateGroup, so it would be the Groupowner in all cases. And then start locals service advertising. Clients then would simply look for the advertisement and once they see it, they would start connection process to the server. One the server connection is made over Wifi direct you would simply start socket communications from the client to the server (server would have listening socket on all times).
Note that connection would require user to click on the dialog showed when client tries to connect to the group owner. And if you want to get rid of this. Then you could actually use the Accesspoint created by GreateGroup, and add the access point name as well as the password to the advertising. Then your clients could actually use the accesspoint to connect (like to any Wlan accesspoint)
Note though that the Wifi Direct way, would not interfere with Wifi connections, not would it require it. But the accesspoint way would mean that any existing Wifi connection from the client would be disconnected, and the device thinks that the connection made to the server would provide normal internet connectivity.
Remember that devices don't need to be connected to a network to connect to each other. Wi-Fi Direct allows them to connect directly.
Here is a list of Wi-Fi Direct resources that you may find useful: https://groups.google.com/forum/#!topic/wi-fi-direct/uWpuOzHY6y0
I'd recommend following Android's Service Discovery Demo and try implementing it yourself. And here is the source code for the demo.
I have to set a priority for wifi configuration sothat I have to always connect to specified wifi network so that device ignores other available wifi in the region.
configuration.priority = 100000;
It is connecting to the last known connected wifi network and ignoring specified wifi network.
If you are trying to set priority for a particular Wifi Configuration, you must use
config.priority = value;
WifiManager.updateNetwork(config);
and you should use the below api after updating
WifiManager.saveConfiguration();
I was able to set priority for my configured network.
Note:
By default the android system will provide highest priority for the last known connected wifi network. If you want to set a higher priority for any other configuration, set the value with respect to the highest value set by the android system rather going for some numerical digit.
Default priority value is in relative with the highest value and its also based on its usage preference. User set value will be changed if the network is manually selected.
If I remember correctly, changes to pre-configured networks should be saved to make an effect.
addOrUpdateNetworkNative(WifiConfiguration config);
I know this is probably a bit late, but I've been looking at the same thing, and apparently 99 999 is the highest value you can set, anything higher than that gets ignored From here
Is it possible to set the IP address of an interface in Android within an application?
I can query the available interfaces and their current addresses using java.net.NetworkInterface, but this doesn't provide a facility to change these. Did I just miss something somewhere, or is it not allowed?
I was hoping to be able to make my application either change or add an alias to one or more of the existing interfaces at runtime on an "off the shelf" device. (2.1/2.2). Ideally I'd like to do this for both IPv4 and IPv6 addresses.
Settings.System includes several flags you can use for this:
WIFI_USE_STATIC_IP
WIFI_STATIC_IP
WIFI_STATIC_NETMASK
WIFI_STATIC_GATEWAY
WIFI_STATIC_DNS1 and WIFI_STATIC_DNS2
You'll also need the android.permission.WRITE_SETTINGS permission declared for your application.
Then in your activity:
final ContentResolver cr = getContentResolver();
Settings.System.putInt(cr, Settings.System.WIFI_USE_STATIC_IP, 1);
Settings.System.putString(cr, Settings.System.WIFI_STATIC_IP, "you.re.ip.addr");
// call putString() for each value to set for your interface
If you want to change the IP address of the carrier's 3G/4G,etc interface, I do not believe this is possible - as it is connected to the carrier and uses their DHCP/security for enabling you to connect and use their services (sort of like changing the external IP of your cable modem without the consent of your ISP).