精华内容
下载资源
问答
  • 问题:以太网状态:IPv4连接:无网络访问权限 无法获取有效的IP地址 现状及尝试: 路由器为小米AX6000插电源恢复出场设置之后只用一根网线连接台式电脑,电脑不能访问路由器本地管理页面,但用CMD命令ping路由器IP ...
  • 然后在系统设置页面有个以太网菜单可以进行IP、网关等设置,如下: 二、那怎么通过代码去设置呢? 1、Android系统在Framework层是有提供操作以太网Api的也就是android.net.EthernetManager,但是Google把Api给隐藏...

    一、对于一些工控行业的Android主板一般都会扩展个以太网接口,也就是可以插网线进行上网;然后在系统设置页面有个以太网菜单可以进行IP、网关等设置,如下:

    在这里插入图片描述
    在这里插入图片描述

    二、那怎么通过代码去设置呢?

    1、Android系统在Framework层是有提供操作以太网Api的也就是android.net.EthernetManager,但是Google把Api给隐藏了;所以最终你是调用不了的
    在这里插入图片描述
    2、那需要怎么操作才能调用到隐藏的Api呢?相信你肯定会立马想到使用反射去调用;对,没错!这里我就是使用的这个方式;当然如果你公司系统层也是自己搞的话就更简单了

    三、先假装如果可以调用到android.net.EthernetManager的Api,那么代码应该怎么写呢?

    • 设置静态IP地址, new LinkAddress()报错是因为源码@hide了
      在这里插入图片描述

    • 设置动态ip地址

    在这里插入图片描述

    四、接下来就是通反射去调用了,代码量就比上面多的多了

    /**
     * 文件名:    EthernetUtil
     * 创建时间:  2020/8/21 on 11:22
     * 描述:     TODO 设置以太网静态ip 动态ip地址
     *
     * @author 阿钟
     */
    
    public class EthernetUtil {
    
        /**
         * 设置以太网动态获取IP
         */
        public static boolean setDynamicIp(Context context) {
            try {
                Class<?> ethernetManagerCls = Class.forName("android.net.EthernetManager");
                //获取EthernetManager实例
                Object ethManager = context.getSystemService("ethernet");
                //创建IpConfiguration
                Class<?> ipConfigurationCls = Class.forName("android.net.IpConfiguration");
                Object ipConfiguration = ipConfigurationCls.newInstance();
                //获取ipAssignment、proxySettings的枚举值
                Map<String, Object> ipConfigurationEnum = getIpConfigurationEnum(ipConfigurationCls);
                //设置ipAssignment
                Field ipAssignment = ipConfigurationCls.getField("ipAssignment");
                ipAssignment.set(ipConfiguration, ipConfigurationEnum.get("IpAssignment.DHCP"));
                //设置proxySettings
                Field proxySettings = ipConfigurationCls.getField("proxySettings");
                proxySettings.set(ipConfiguration, ipConfigurationEnum.get("ProxySettings.NONE"));
                //获取EthernetManager的setConfiguration()
                Method setConfigurationMethod = ethernetManagerCls.getDeclaredMethod("setConfiguration", ipConfiguration.getClass());
                //设置动态IP
                setConfigurationMethod.invoke(ethManager, ipConfiguration);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 设置以太网静态IP地址
         *
         * @param address ip地址
         * @param mask    子网掩码
         * @param gate    网关
         * @param dns     dns
         */
        public static boolean setEthernetStaticIp(Context context, String address, String mask, String gate, String dns) {
            try {
                Class<?> ethernetManagerCls = Class.forName("android.net.EthernetManager");
                //获取EthernetManager实例
                Object ethManager = context.getSystemService("ethernet");
                //创建StaticIpConfiguration
                Object staticIpConfiguration = newStaticIpConfiguration(address, gate, mask, dns);
                //创建IpConfiguration
                Object ipConfiguration = newIpConfiguration(staticIpConfiguration);
                //获取EthernetManager的setConfiguration()
                Method setConfigurationMethod = ethernetManagerCls.getDeclaredMethod("setConfiguration", ipConfiguration.getClass());
                //保存静态ip设置
                saveIpSettings(context, address, mask, gate, dns);
                //设置静态IP
                setConfigurationMethod.invoke(ethManager, ipConfiguration);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
    
        /**
         * 获取StaticIpConfiguration实例
         */
        private static Object newStaticIpConfiguration(String address, String gate, String mask, String dns) throws Exception {
            Class<?> staticIpConfigurationCls = Class.forName("android.net.StaticIpConfiguration");
            //实例化StaticIpConfiguration
            Object staticIpConfiguration = staticIpConfigurationCls.newInstance();
            Field ipAddress = staticIpConfigurationCls.getField("ipAddress");
            Field gateway = staticIpConfigurationCls.getField("gateway");
            Field domains = staticIpConfigurationCls.getField("domains");
            Field dnsServers = staticIpConfigurationCls.getField("dnsServers");
            //设置ipAddress
            ipAddress.set(staticIpConfiguration, newLinkAddress(address, mask));
            //设置网关
            gateway.set(staticIpConfiguration, InetAddress.getByName(gate));
            //设置掩码
            domains.set(staticIpConfiguration, mask);
            //设置dns
            ArrayList<InetAddress> dnsList = (ArrayList<InetAddress>) dnsServers.get(staticIpConfiguration);
            dnsList.add(InetAddress.getByName(dns));
            return staticIpConfiguration;
        }
    
        /**
         * 获取LinkAddress实例
         */
        private static Object newLinkAddress(String address, String mask) throws Exception {
            Class<?> linkAddressCls = Class.forName("android.net.LinkAddress");
            Constructor<?> linkAddressConstructor = linkAddressCls.getDeclaredConstructor(InetAddress.class, int.class);
            return linkAddressConstructor.newInstance(InetAddress.getByName(address), getPrefixLength(mask));
        }
    
        /**
         * 获取IpConfiguration实例
         */
        private static Object newIpConfiguration(Object staticIpConfiguration) throws Exception {
            Class<?> ipConfigurationCls = Class.forName("android.net.IpConfiguration");
            Object ipConfiguration = ipConfigurationCls.newInstance();
            //设置StaticIpConfiguration
            Field staticIpConfigurationField = ipConfigurationCls.getField("staticIpConfiguration");
            staticIpConfigurationField.set(ipConfiguration, staticIpConfiguration);
            //获取ipAssignment、proxySettings的枚举值
            Map<String, Object> ipConfigurationEnum = getIpConfigurationEnum(ipConfigurationCls);
            //设置ipAssignment
            Field ipAssignment = ipConfigurationCls.getField("ipAssignment");
            ipAssignment.set(ipConfiguration, ipConfigurationEnum.get("IpAssignment.STATIC"));
            //设置proxySettings
            Field proxySettings = ipConfigurationCls.getField("proxySettings");
            proxySettings.set(ipConfiguration, ipConfigurationEnum.get("ProxySettings.STATIC"));
            return ipConfiguration;
        }
    
        /**
         * 获取IpConfiguration的枚举值
         */
        private static Map<String, Object> getIpConfigurationEnum(Class<?> ipConfigurationCls) {
            Map<String, Object> enumMap = new HashMap<>();
            Class<?>[] enumClass = ipConfigurationCls.getDeclaredClasses();
            for (Class<?> enumC : enumClass) {
                Object[] enumConstants = enumC.getEnumConstants();
                if (enumConstants == null) continue;
                for (Object enu : enumConstants) {
                    enumMap.put(enumC.getSimpleName() + "." + enu.toString(), enu);
                }
            }
            return enumMap;
        }
    
        /**
         * 保存静态ip设置
         */
        private static void saveIpSettings(Context context, String address, String mask, String gate, String dns) {
            ContentResolver contentResolver = context.getContentResolver();
            Settings.Global.putString(contentResolver, "ethernet_static_ip", address);
            Settings.Global.putString(contentResolver, "ethernet_static_mask", mask);
            Settings.Global.putString(contentResolver, "ethernet_static_gateway", gate);
            Settings.Global.putString(contentResolver, "ethernet_static_dns1", dns);
        }
    
        /**
         * 获取长度
         */
        private static int getPrefixLength(String mask) {
            String[] strs = mask.split("\\.");
            int count = 0;
            for (String str : strs) {
                if (str.equals("255")) {
                    ++count;
                }
            }
            return count * 8;
        }
    }
    
    • 使用只需调用方法即可:
    //设置静态ip
    EthernetUtil.setEthernetStaticIp(context, "192.168.5.33", "255.255.255.0", "192.168.5.1", "8.8.8.8");
    //设置动态获取ip
    EthernetUtil.setDynamicIp(context)
    

    最后:这个代码本人在Android7.0的原生系统验证是可以使用的,有需要的可以使用试试EthernetUtil源码下载地址

    展开全文
  • 以太网自动获取IP总不是自己家网的IP网段怎么解决?请各位大牛帮帮忙
  • 软件版本:android6.0硬件平台:MTK6797 近来,遇到了一个问题,板子这边通过以太网上网,一开始能获取ip并且上网,但是一段时间后,网络会断掉,究其原因是ip地址没有了,由于之前接触过dhcp相关的内容,首先查看...

    软件版本:android6.0

    硬件平台:MTK6797

            近来,遇到了一个问题,板子这边通过以太网上网,一开始能获取ip并且上网,但是一段时间后,网络会断掉,究其原因是ip地址没有了,由于之前接触过dhcp相关的内容,首先查看系统中dhcp的运行状态:

    命令行运行:getprop | grep dhcp 查看dhcp相关属性,结果如下:

    [dhcp.eth0.result]: [failed]
    [init.svc.dhcpcd_eth0]: [stopped]

    运行ps | grep dhcpcd  结果为空,说明系统的dhcpcd守护进程挂了。因此没有ip地址很正常。

    于是查看init.mt6797.rc中关于dhcpcd的service定义:

    service dhcpcd_eth0 /system/bin/dhcpcd -aABDKL
         class late_start

         disabled

         oneshot

        但是在dhcpcd为stopped时,并没有重启,因此没有ip也就正常了,所以需要做一个条件触发监测,在dhcpcd停止之后,重新拉起该服务,因此在此脚本文件末尾添加如下:

     on property:init.svc.dhcpcd_eth0=stopped
         start dhcpcd_eth0

    意为在dhcpcd停掉之后,重启该服务。

        本想着问题解决了,打算自嗨片刻,结果TM烧录进去一试,还是特码获取不了,而且这次的现象是dhcp虽然是stopped状态,但是一直被拉起,最后却一直拉起失败,所以这次问题应该在这里了。

    查看了系统日志:logcat就是init打印的关于start dhcpcd_eth0但是exit with status 1的日志,总之没开启来,由于加了属性触发,一直被start,却从未成功。

    dmesg的日志有关于

    dhcpcd:Android requires an interface

    dhcpcd:stop_control:No such file or directory

    发现这个日志是由于dhcpcd需要指定获取IP的网卡,那么在哪里指定呢,依然是init.mt6797.rc

    service dhcpcd_eth0 /system/bin/dhcpcd -aABDKL 将这一行改为:

    service dhcpcd_eth0 /system/bin/dhcpcd -aABDKL -d eth0

    然后编译,烧录,问题解决!!!!



    好久不说这俩字了。。。谮棒!!!!!!










    展开全文
  • 设置中设置以太网为静态ip,设置后使用ifconfig查看ip显示正常。然后重启,极大概率出现开机后设备没有获取ip. 分析解决 测试后发现了几点规律: 设置静态ip会开机获取不到ip,但是设置动态ip无问题 没有获取ip...

    测试平台

    Platform: RK3399/RK3328
    OS: Android 10.0

    现象

    设置中设置以太网为静态ip,设置后使用ifconfig查看ip显示正常。然后重启,极大概率出现开机后设备没有获取到ip.

    分析解决

    测试后发现了几点规律:

    1. 设置静态ip会开机获取不到ip,但是设置动态ip无问题
    2. 没有获取到ip时,使用ifconfig查看,发现网卡设备连接正常。此时拔插网线,或者使用ifconfig eth0 down + ifconfig eth0 up 来开关一次设备后,就能正常获取到ip

    根据测试结果采取了如下的解决方案,在开机的时候在Ethernet服务中进行一次ifconfig eth0 down + ifconfig eth0 up的操作来解决.

    修改补丁如下,测试20次后无问题,提交给到客户

    diff --git a/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetTracker.java b/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetTracker.java
    index 308e328..919edc6 100644
    --- a/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetTracker.java
    +++ b/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetTracker.java
    @@ -252,12 +254,41 @@ final class EthernetTracker {
             }
         }
     
    -    private void maybeTrackInterface(String iface) {
    +    private boolean maybeTrackInterface(String iface) {
             if (DBG) Log.i(TAG, "maybeTrackInterface " + iface);
             // If we don't already track this interface, and if this interface matches
             // our regex, start tracking it.
             if (!iface.matches(mIfaceMatch) || mFactory.hasInterface(iface)) {
    -            return;
    +            return false;
             }
     
             if (mIpConfigForDefaultInterface != null) {
    @@ -266,13 +297,31 @@ final class EthernetTracker {
             }
     
             addInterface(iface);
    +        return true;
         }
     
         private void trackAvailableInterfaces() {
             try {
                 final String[] ifaces = mNMService.listInterfaces();
                 for (String iface : ifaces) {
    -                maybeTrackInterface(iface);
    +               if (maybeTrackInterface(iface)) {
    +                    String mIfaceTmp = iface;
    +                    new Thread(new Runnable() {
    +                        public void run() {
    +                            // carrier is always 1 when kernel boot up no matter RJ45 plugin or not,
    +                            // sleep a little time to wait kernel's correct carrier status
    +                            try {
    +                                Thread.sleep(3000);
    +                            } catch (InterruptedException ignore) {
    +                            }
    +							if(isEthernetInterfaceActive()){
    +                                IpConfiguration config = getIpConfiguration(mIfaceTmp);
    +                                if(config != null && IpAssignment.STATIC == config.getIpAssignment())
    +                                {
    +                                    updateInterfaceState(mIfaceTmp, false);
    +                                    updateInterfaceState(mIfaceTmp, true);
    +                                }
    +                            }else{
    +                                 updateInterfaceState(mIfaceTmp, false);
    +                             }
    +                        }
    +                    }).start();
    +                    break;
    +                }
                 }
             } catch (RemoteException | IllegalStateException e) {
                 Log.e(TAG, "Could not get list of interfaces " + e);
    
    
    

    注:其实这是种治标不治本的解决办法,但是由于客户追的比较紧先采取这种解决方案,后续有时间再去查一下根本所在(根据测试log等,我猜想这是个由于加载顺序和获取ip的时机导致的随机问题)

    展开全文
  • 判断网络是否可用: 注意!是判断网络是否可用,但网络可用不代表一定能上外网的! public static boolean isNetworkAvailable(Context context) { ConnectivityManager manager = (ConnectivityManager) context...
  • /*** 文件名: EthernetUtil* 创建时间: 2020/8/21 on 11:22* 描述: TODO 设置以太网静态ip 动态ip地址** @author 阿钟*/public class EthernetUtil {/*** 设置以太网动态获取IP*/public static boolean setDynamicIp...

    /**

    * 文件名: EthernetUtil

    * 创建时间: 2020/8/21 on 11:22

    * 描述: TODO 设置以太网静态ip 动态ip地址

    *

    * @author 阿钟

    */

    public class EthernetUtil {

    /**

    * 设置以太网动态获取IP

    */

    public static boolean setDynamicIp(Context context) {

    try {

    Class> ethernetManagerCls = Class.forName("android.net.EthernetManager");

    //获取EthernetManager实例

    Object ethManager = context.getSystemService("ethernet");

    //创建IpConfiguration

    Class> ipConfigurationCls = Class.forName("android.net.IpConfiguration");

    Object ipConfiguration = ipConfigurationCls.newInstance();

    //获取ipAssignment、proxySettings的枚举值

    Map ipConfigurationEnum = getIpConfigurationEnum(ipConfigurationCls);

    //设置ipAssignment

    Field ipAssignment = ipConfigurationCls.getField("ipAssignment");

    ipAssignment.set(ipConfiguration, ipConfigurationEnum.get("IpAssignment.DHCP"));

    //设置proxySettings

    Field proxySettings = ipConfigurationCls.getField("proxySettings");

    proxySettings.set(ipConfiguration, ipConfigurationEnum.get("ProxySettings.NONE"));

    //获取EthernetManager的setConfiguration()

    Method setConfigurationMethod = ethernetManagerCls.getDeclaredMethod("setConfiguration", ipConfiguration.getClass());

    //设置动态IP

    setConfigurationMethod.invoke(ethManager, ipConfiguration);

    return true;

    } catch (Exception e) {

    e.printStackTrace();

    return false;

    }

    }

    /**

    * 设置以太网静态IP地址

    *

    * @param address ip地址

    * @param mask 子网掩码

    * @param gate 网关

    * @param dns dns

    */

    public static boolean setEthernetStaticIp(Context context, String address, String mask, String gate, String dns) {

    try {

    Class> ethernetManagerCls = Class.forName("android.net.EthernetManager");

    //获取EthernetManager实例

    Object ethManager = context.getSystemService("ethernet");

    //创建StaticIpConfiguration

    Object staticIpConfiguration = newStaticIpConfiguration(address, gate, mask, dns);

    //创建IpConfiguration

    Object ipConfiguration = newIpConfiguration(staticIpConfiguration);

    //获取EthernetManager的setConfiguration()

    Method setConfigurationMethod = ethernetManagerCls.getDeclaredMethod("setConfiguration", ipConfiguration.getClass());

    //保存静态ip设置

    saveIpSettings(context, address, mask, gate, dns);

    //设置静态IP

    setConfigurationMethod.invoke(ethManager, ipConfiguration);

    return true;

    } catch (Exception e) {

    e.printStackTrace();

    return false;

    }

    }

    /**

    * 获取StaticIpConfiguration实例

    */

    private static Object newStaticIpConfiguration(String address, String gate, String mask, String dns) throws Exception {

    Class> staticIpConfigurationCls = Class.forName("android.net.StaticIpConfiguration");

    //实例化StaticIpConfiguration

    Object staticIpConfiguration = staticIpConfigurationCls.newInstance();

    Field ipAddress = staticIpConfigurationCls.getField("ipAddress");

    Field gateway = staticIpConfigurationCls.getField("gateway");

    Field domains = staticIpConfigurationCls.getField("domains");

    Field dnsServers = staticIpConfigurationCls.getField("dnsServers");

    //设置ipAddress

    ipAddress.set(staticIpConfiguration, newLinkAddress(address, mask));

    //设置网关

    gateway.set(staticIpConfiguration, InetAddress.getByName(gate));

    //设置掩码

    domains.set(staticIpConfiguration, mask);

    //设置dns

    ArrayList dnsList = (ArrayList) dnsServers.get(staticIpConfiguration);

    dnsList.add(InetAddress.getByName(dns));

    return staticIpConfiguration;

    }

    /**

    * 获取LinkAddress实例

    */

    private static Object newLinkAddress(String address, String mask) throws Exception {

    Class> linkAddressCls = Class.forName("android.net.LinkAddress");

    Constructor> linkAddressConstructor = linkAddressCls.getDeclaredConstructor(InetAddress.class, int.class);

    return linkAddressConstructor.newInstance(InetAddress.getByName(address), getPrefixLength(mask));

    }

    /**

    * 获取IpConfiguration实例

    */

    private static Object newIpConfiguration(Object staticIpConfiguration) throws Exception {

    Class> ipConfigurationCls = Class.forName("android.net.IpConfiguration");

    Object ipConfiguration = ipConfigurationCls.newInstance();

    //设置StaticIpConfiguration

    Field staticIpConfigurationField = ipConfigurationCls.getField("staticIpConfiguration");

    staticIpConfigurationField.set(ipConfiguration, staticIpConfiguration);

    //获取ipAssignment、proxySettings的枚举值

    Map ipConfigurationEnum = getIpConfigurationEnum(ipConfigurationCls);

    //设置ipAssignment

    Field ipAssignment = ipConfigurationCls.getField("ipAssignment");

    ipAssignment.set(ipConfiguration, ipConfigurationEnum.get("IpAssignment.STATIC"));

    //设置proxySettings

    Field proxySettings = ipConfigurationCls.getField("proxySettings");

    proxySettings.set(ipConfiguration, ipConfigurationEnum.get("ProxySettings.STATIC"));

    return ipConfiguration;

    }

    /**

    * 获取IpConfiguration的枚举值

    */

    private static Map getIpConfigurationEnum(Class> ipConfigurationCls) {

    Map enumMap = new HashMap<>();

    Class>[] enumClass = ipConfigurationCls.getDeclaredClasses();

    for (Class> enumC : enumClass) {

    Object[] enumConstants = enumC.getEnumConstants();

    if (enumConstants == null) continue;

    for (Object enu : enumConstants) {

    enumMap.put(enumC.getSimpleName() + "." + enu.toString(), enu);

    }

    }

    return enumMap;

    }

    /**

    * 保存静态ip设置

    */

    private static void saveIpSettings(Context context, String address, String mask, String gate, String dns) {

    ContentResolver contentResolver = context.getContentResolver();

    Settings.Global.putString(contentResolver, "ethernet_static_ip", address);

    Settings.Global.putString(contentResolver, "ethernet_static_mask", mask);

    Settings.Global.putString(contentResolver, "ethernet_static_gateway", gate);

    Settings.Global.putString(contentResolver, "ethernet_static_dns1", dns);

    }

    /**

    * 获取长度

    */

    private static int getPrefixLength(String mask) {

    String[] strs = mask.split("\\.");

    int count = 0;

    for (String str : strs) {

    if (str.equals("255")) {

    ++count;

    }

    }

    return count * 8;

    }

    }

    展开全文
  • 局域网的配置,之前查找了很多方法都是关于EthernetManager(已隐藏,无法直接调用),试过很多种但是还是没成功调到过这个对象,然后直接利用adb 命令获取 获取以太网的连接信息 1.获取所有有效网关 /** * @return ...
  • 安卓获取以太网IP地址

    千次阅读 2013-12-05 21:54:35
    闲话不说,直接上代码: public static String getLocalIpAddress() {  try {  for (Enumeration en = NetworkInterface  .getNetworkInterfaces(); en.hasMoreElements();) {
  • Android 获取以太网Mac地址和IP地址

    万次阅读 2016-01-29 16:11:57
    tempIpInfo = SystemProperties.get("dhcp." + iface + ".ipaddress"); if ((tempIpInfo != null) && (!tempIpInfo.equals(""))) { mEthIpAddress = tempIpInfo; } else { mEthIpAddress = ...
  • win10开启移动热点,手机无法获取ip地址

    万次阅读 多人点赞 2020-01-03 14:40:34
    win10打开移动热点后,手机一直显示正在获取ip地址。 1.确认wwan autoconfig/wlan autoconfig启动 2.设置网线网卡的共享里的勾选选中,连接选择启用热点后多出来的那个连接 插网线的网卡-->共享-->勾选...
  • 代码下载地址:https://pan.baidu.com/s/15-LLGdTA46jgDyqQcx6SUA  上次写过一遍这一篇的,复制粘贴时studio,有背景色,好... 1:开机自启,每30秒去检测当前的网络状态 ,因为我的需求是以太网,所有其他的网络...
  • 施耐德Quantum、Premium/Twido和M340系列PLC的以太网端口如果没有配置IP地址,那么该设备的缺省IP可以通过MAC地址转化得到。 例如,如果以太网端口的MAC地址为00.00.54.10.11.12,那么这三种PLC的以太网端口的缺省...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 40,212
精华内容 16,084
关键字:

以太网无法获取ip