Ethernet Connectivity through Programmatically (Android) (Rooted Device)

Manohar Perepa picture Manohar Perepa · Feb 13, 2014 · Viewed 29.4k times · Source

I have a small issue regarding Ethernet.

My three questions are:

  1. Can we programmatically Turn-On/Off Ethernet?

  2. Can we programmatically Enable/Disable Ethernet?

  3. Can we programmatically Connect Ethernet?

The above Questions are done with the Wifi. Like

  1. We can programmatically Turn-On/Off Wifi.

  2. We can programmatically Enable/Disable Wifi.

  3. We can programmatically Connect Wifi using WifiManager.

Does android provides any EthernetManager like as WifiManager to handle Ethernet?

Or, if this doesn't seem feasible, then my original requirement is:

The first thing I am going to clear is "DEVICE IS ROOTED" .

Can I manipulate the Settings (Default)? Like I don't want any other option in the Settings.apk other than WIFI and Ethernet. It should show only Wifi and Ethernet. That's it. Can I disable all the options from the Settings or Can I remove all the other options from the Settings?

Answer

Robin Gawenda picture Robin Gawenda · Apr 10, 2014

The solution I will present here is a hack using reflection and does only work on a rooted android system.

Your device might have the popular android.net.ethernet package. In an Activity, try

Object emInstance = getSystemService("ethernet");

It returns an valid instance of the EthernetManager or null. Null means you are out of luck.

An additional requirement might be depending on your device: Ethernet and Wifi might only work exclusively. You might need to disable Wifi to enable Ethernet and vice versa.

To enable Ethernet by reflection use your instance of the EthernetManager. The method you want to invoke is setEthEnabled(boolean enabled)

    Class<?> emClass = null;
    try {
        emClass = Class.forName("android.net.ethernet.EthernetManager");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Object emInstance = getSystemService("ethernet");

    Method methodSetEthEnabled = null;
    try {
        methodSetEthEnabled = emClass.getMethod("setEthEnabled", Boolean.TYPE);
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    methodSetEthEnabled.setAccessible(true);
    try {
        // new Boolean(true) to enable, new Boolean(false) to disable
        methodSetEthEnabled.invoke(emInstance, new Boolean(false));
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Your application manifest needs these permissions

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

The permission WRITE_SECURE_SETTINGS can only be acquired by system apps. The app does not need to be signed by a system key. It can be any valid sign (like the regular Android App Export function). Use busybox to remount the system partition for write access and move your apk into the /system/app folder. Reboot the device and it should work.

Can we programmatically Connect Ethernet ?

There is no Access Point to connect you like with Wifi. You either configure it for DHCP or provide static values. This can of course also be done via reflection. You will need the class EthernetDevInfo for that.

The actual implementation of the EthernetManager and EthernetDevInfo might slightly differ between Android versions and devices as it doesn't have to conform to a public api (yet) and might even be a custom version. To get a list of getters and setters you can use a Introspector or reflection in general.