通过编程方式实现以太网连接(Android)(Root设备)

Man*_*epa 19 android ethernet android-networking

我有一个关于以太网的小问题.

我的三个问题是:

  1. 我们可以通过编程方式开启/关闭Ethernet吗?

  2. 我们可以编程启用/禁用Ethernet吗?

  3. 我们能以编程方式连接Ethernet吗?

上述问题是通过以下方式完成的Wifi.喜欢

  1. 我们可以通过编程方式开启/关闭Wifi.

  2. 我们可以通过编程方式启用/禁用Wifi.

  3. 我们可以通过编程连接Wifi使用WifiManager.

android是否提供任何EthernetManager类似WifiManager处理Ethernet

或者,如果这似乎不可行,那么我原来的要求是:

我要澄清的第一件事是"设备已经生根".

我可以操作设置(默认)吗?就像我不希望Settings.apk除了WIFI和之外的任何其他选项Ethernet.它应该只显示WifiEthernet.而已.我可以从"设置"中禁用所有选项,还是可以从"设置"中删除所有其他选项?

Rob*_*nda 13

我将在这里介绍的解决方案是使用反射的hack,并且只适用于root系统.

您的设备可能有流行的android.net.ethernet包.在活动中,试试

Object emInstance = getSystemService("ethernet");
Run Code Online (Sandbox Code Playgroud)

它返回EthernetManager的有效实例或null.Null意味着你运气不好.

另外一项要求可能取决于您的设备:以太网和Wifi可能仅适用于您的设备.您可能需要禁用Wifi以启用以太网,反之亦然.

要通过反射启用以太网,请使用EthernetManager的实例.要调用的方法是setEthEnabled(启用布尔值)

    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();
    }
Run Code Online (Sandbox Code Playgroud)

您的应用程序清单需要这些权限

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
Run Code Online (Sandbox Code Playgroud)

WRITE_SECURE_SETTINGS权限只能由系统应用获取.该应用程序不需要由系统密钥签名.它可以是任何有效的标志(如常规的Android App Export功能).使用busybox重新安装系统分区以进行写访问,并将apk移至/ system/app文件夹.重新启动设备,它应该工作.

我们可以编程连接以太网吗?

没有接入点可以与Wifi连接.您可以将其配置为DHCP或提供静态值.这当然也可以通过反射来完成.您将需要EthernetDevInfo类.

EthernetManager和EthernetDevInfo的实际实现可能在Android版本和设备之间略有不同,因为它不必符合公共API(还),甚至可能是自定义版本.要获取getter和setter列表,您可以使用Introspector或反射.


MTu*_*ash 11

好的,这里是我用ETHERNET INTERFACE(eth0)操作的一些方法.

1)检查以太网接口是否存在的方法

public static boolean doesEthExist() {
        List<String> list = getListOfNetworkInterfaces();

        return list.contains("eth0");
    }

public static List<String> getListOfNetworkInterfaces() {

        List<String> list = new ArrayList<String>();

        Enumeration<NetworkInterface> nets;

        try {
            nets = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {

            e.printStackTrace();
            return null;
        }

        for (NetworkInterface netint : Collections.list(nets)) {

            list.add(netint.getName());
        }

        return list;

    }
Run Code Online (Sandbox Code Playgroud)

2)检查ETHERNET是启用还是启用的方法

public static boolean isEthOn() {

        try {

            String line;
            boolean r = false;

            Process p = Runtime.getRuntime().exec("netcfg");

            BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream()));   
            while ((line = input.readLine()) != null) {   

                if(line.contains("eth0")){
                    if(line.contains("UP")){
                        r=true;
                    }
                    else{
                        r=false;
                    }
                }
            }   
            input.close();

            Log.e("OLE","isEthOn: "+r);
            return r; 

        } catch (IOException e) {
            Log.e("OLE","Runtime Error: "+e.getMessage());
            e.printStackTrace();
            return false;
        }

    }
Run Code Online (Sandbox Code Playgroud)

3)根据其状态启用或禁用以太网的方法

public static void turnEthOnOrOff() {

        try {

            if(isEthOn()){
                Runtime.getRuntime().exec("ifconfig eth0 down");

            }
            else{
                Runtime.getRuntime().exec("ifconfig eth0 up");
            }

        } catch (IOException e) {
            Log.e("OLE","Runtime Error: "+e.getMessage());
            e.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

4)根据所选类型通过以太网连接的方法(dhcp/static)

private boolean connectToStaticSettingsViaIfconfig(StaticConnectionSettings scs) {


        try {

            if(typeChosen.equalsIgnoreCase("dhcp")){

                Runtime.getRuntime().exec("ifconfig eth0 dhcp start");
            }
            else{

                Runtime.getRuntime().exec("ifconfig eth0 "+scs.getIp()+" netmask "+scs.getNetmask()+" gw "+scs.getGateway());
            }

        } catch (IOException e) {
            Log.e("OLE","Runtime Error: "+e.getMessage());
            e.printStackTrace();
            return false;
        }

        return true;
    }
Run Code Online (Sandbox Code Playgroud)

我创建了另外一个类来存储所需的所有eth值.然后使用用户插入的值初始化此类.

public class StaticConnectionSettings {

    private String ip, netmask, dns, mac, gateway, type;

//Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)

这就是......我将很快测试它......这段代码缺少测试阶段(ping).也许它需要设置DNS.但这可以很容易地完成.我没有把它包括在内,因为我认为在我们的设备上它也可以在没有DNS设置的情况下工作.


MTu*_*ash 1

对您上述问题的三个回答:

  1. 是的。你可以尝试使用ifconfig eth0 down ; ifconfig eth0 up. 但我还没有亲自测试过。
  2. 是的,但您不必这样做。Android 会为您进行切换。如果您连接到 WiFi,以太网将禁用。如果您已经连接到 WiFi 并将以太网线插入设备;您只需要禁用 WiFi(您知道如何操作),Android 就会自动切换到以太网。
  3. 并不像您想象的那么容易。我有同样的问题,到目前为止我只找到了一种尚未测试的解决方案。由于android运行在linux内核上,我们可以使用ifconfig来操作以太网连接。

这里隐藏了解释: http://elinux.org/images/9/98/Dive_Into_Android_Networking-_Adding_Ethernet_Connectivity.pdf

以及本次讲座的youtube视频

http://www.youtube.com/watch?v=LwI2NBq7BWM

以及关于如何在 android 上使用 ifconfig 的参考

Android 以太网使用 dhcp 配置 IP

因此,如果您找到可能的解决方案,请分享!如果我能在你之前做到这一点,我一定会的。