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.