Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Ethernet - Programmatically

I am developing an Android app which will be used by custom devices which will have ethernet support (and also wifi).

The app has to enable a settings activity for Ethernet.

Please NOTE that these settings have to be run by the app and not by the Android settings, since the app will be the only thing running on the device and the user will not have access to the Android running in the background.

The user has to be able to:

  1. ENABLE/DISABLE Ethernet
  2. Choose DHCP or STATIC
  3. If choosing STATIC - set IP, gateway

The problem is that I cannot access the android.net.ethernet programmatically and there is no explanation about this issue online.

So if someone has done something like this, please help me get into the right direction.

like image 313
MTurPash Avatar asked Feb 14 '14 09:02

MTurPash


1 Answers

I know it is very late but it might help someone else.

I had some of the requirements you mentioned for my android application. This is how I achieved some of the points

1. ENABLE/DISABLE Ethernet

//Enable Ethernet

 ifconfig eth0 up

//Disable Ethernet

 ifconfig eth0 down

3. If chosen STATIC - set IP, gateway

Fire these commands from java code.

su -c ifconfig eth0 172.19.10.105 netmask 255.255.255.0 up
route add default gw 172.19.10.2 dev eth0

You can execute these commands using following code.

Here command variable is one of the commands mentioned above.

                Process p;
                try {
                    p = Runtime.getRuntime().exec(command);
                    p.waitFor();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

                    String line = "";
                    while ((line = reader.readLine())!= null) {
                        output.append(line + "n");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                String response = output.toString();
like image 200
Tushar Thakur Avatar answered Sep 28 '22 08:09

Tushar Thakur