Set static ip to ethernet in android

12,156

Solution 1

I was able to set Ip to Ethernet connection as follows. I was using Allwinner A31s android board.

String command1 = "su -c ifconfig eth0 "
                        + terminalIpAddressString+" netmask "
                        + subnetMaskAddressString
                        +" up";
                String command2 = "route add default gw "
                        + gatewayAddressString+" dev eth0";



                String command3 = "mount -o remount,rw /system";
                String command4 = "echo \"su -c ifconfig eth0 "
                        +terminalIpAddressString+" netmask "
                        +subnetMaskAddressString+" up;" +
                        "route add default gw "
                        +gatewayAddressString
                        +" dev eth0\" > /system/bin/preinstall.sh";

                String command5 = "busybox sed -i 's/su -c ifconfig eth0 "
                        +terminalIpAddressString
                        +" netmask "+subnetMaskAddressString+" up;"
                        +"route add default gw 172.19.10.2 dev eth0"
                        + "/su -c ifconfig eth0 "+terminalIpAddressString
                        +" netmask "+subnetMaskAddressString+" up;"
                        +"route add default gw "+gatewayAddressString
                        +" dev eth0/g' /system/bin/preinstall.sh";

Solution 2

You may need to create /data/misc/ethernet/ipconfig.txt file to configure static IP address.
As you successfully configured static IP address for WiFi already,
I think /data/misc/wifi/ipconfig.txt was also created and it will be valid for Ethernet config, too.
Please refer to following links for file path and data format.
EthernetConfigStore.java
IpConfigStore.java

Share:
12,156
Tushar Thakur
Author by

Tushar Thakur

I am a Softwar Developer

Updated on June 28, 2022

Comments

  • Tushar Thakur
    Tushar Thakur almost 2 years

    I have ethernet connected to my android board. I want to manually set IP from code. I was able to set IP address for WIFI. I have looked into following links for ethernet

    Assign static IP to ethernet card from OTG So far I have not found how to set static IP to ethernet via android code.