Android 13 以太网开发总结

Android 13 以太网开发总结

前言

相较于Android12,Android13将以太网相关功能整合到ConnectivityService里,将以太网的核心源码从framework上移到packages/modules/Connectivity下,功能也做了相关更新。


一、具体变更

1、对于Android13以前的版本的方法做了限制,不允许Android13以上的版本使用。如设置以太网参数的方法和获取以太网参数的方法。

2、增加了以太网开关的方法setEthernetEnabled()。

3、新增了车机项目更新以太网参数的方法updateConfiguration()。

4、新增以太网状态变更通知机制。

二、合入mainline后以太网开发思路

1、由于合入mainline后,ConnectivityService会被替换,以前开发使用的在EthernetService里实现新接口的方法行不通了。

2、如何获取以太网状态变更:

以前是通过判断以太网节点eth0是否存在来判断以太网是否连接,在Android13上是行不通的。但是通过阅读源码发现,Android13版本的以太网帮你实现了以太网状态变更后通知的机制,当EthernetService处理完以太网插拔事件、网络配置更新事件、以太网开启关闭事件后会主动调用
InterfaceStateListener通知所有监听。
通过实现他的接口就可监听以太网状态的变更,可以监听到以太网的状态state、路由role、网络配置IpConfiguration 。

    public interface InterfaceStateListener {

        /**
         * Called when an Ethernet interface changes state.
         *
         * @param iface the name of the interface.

         * @param state the current state of the interface, or {@link #STATE_ABSENT} if the
         *              interface was removed.
         * @param role whether the interface is in client mode or server mode.

         * @param configuration the current IP configuration of the interface.
         * @hide
         */

        @SystemApi(client = MODULE_LIBRARIES)
        void onInterfaceStateChanged(@NonNull String iface, @InterfaceState int state,
                @Role int role, @Nullable IpConfiguration configuration);
    }

     mEthernetListener = new EthernetManager.InterfaceStateListener() {
         @Override
         public void onInterfaceStateChanged(@NonNull String iface, int state, int role,@NonNull IpConfiguration configuration) {

                 //do somthing

         }
     };

3、如何调用已经禁止使用的方法:getConfiguration() setConfiguration()

Google通过

@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)

来禁止不符合条件的app和service来调用它的方法。简单来说就是如果api高于Android11 ,应用就无法使用以前的接口设置有线网配置。

如果可以修改源码,当然有很多方法来越过这些限制,比如实现一个名字不同功能一样的接口,这样既不影响CTS测试,又可以轻松的完成工作。但是上文所说Android13以太网相关功能被整合到ConnectivityService里,而mainline会替换ConnectivityService,导致无法修改源码。

为了绕开限制,作者也做过很多尝试,但都行不通。Android这几年框架变化很快,越来越符合我们刚学习编程时老师教我们的那句思想:”高内聚,低耦合“,也就是模块化越来越明显了,深入学习Android编译规则在工作中也会更加轻松。好了,诸多方法行不通后,我们还有最后的大招:“反射”,亲测反射还是可以正常调用这些做了限制的方法。

    private IpConfiguration getConfiguration(String iface){
        IpConfiguration mIpConfiguration = null;
        try{
            Class<? extends EthernetManager> c = mEthManager.getClass();

            Method method = c.getMethod("getConfiguration", String.class);
            EthernetManager tempManager = mEthManager;
            method.setAccessible(true);

            Log.e(TAG,"get getConfiguration Method: " + (method == null));
            Object Values = method.invoke(tempManager,iface);
            mIpConfiguration = (IpConfiguration)Values;

        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            Log.e(TAG,"getDeclaredMethod: " + e.getMessage());
        }
        return mIpConfiguration;
    }
    private void setConfiguration(String iface ,IpConfiguration config){
        try{
            Class<? extends EthernetManager> c = mEthManager.getClass();
/*          Method[] methods = c.getDeclaredMethods();

            for (Method m : methods){
                Log.e(TAG,"getDeclaredMethod1111: " + m.getName());
            } */

            Method method = c.getMethod("setConfiguration", String.class ,IpConfiguration.class);
            EthernetManager tempManager = mEthManager;
            method.setAccessible(true);

            Log.e(TAG,"get setConfiguration Method: " + (method == null));
            method.invoke(tempManager,iface, config);

        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            Log.e(TAG,"getDeclaredMethod: " + e.getMessage());
        }
    }

总结

例如:以上就是今天要讲的内容,个人见解,如有不对请指出。

Android 13以太网是指在Android 13操作系统中支持以太网连接的功能。以太网是一种常见的局域网连接技术,通过物理电缆将计算机设备连接起来,实现快速、高带宽的数据传输。 Android 13以太网功能使得用户可以通过有线连接方式将Android设备与局域网中的其他设备连接起来,并实现高速的数据传输。这样的功能在某些场景下非常有用,比如在需要高速传输大量数据的工作环境中,或者在需要连接到不稳定或无线网络信号的地方。 使用Android 13以太网功能的步骤相对简单。首先,需要使用一个以太网适配器来将Android设备连接到局域网。然后,在Android设备的设置菜单中,找到网络设置选项并进入以太网设置界面。在该界面上,用户可以配置本地网络信息,如IP地址、子网掩码、网关地址等。 一旦以太网连接设置完成,Android设备就能够和局域网中的其他设备进行通信了。用户可以通过以太网连接访问内部网络资源,比如共享文件夹、打印机、服务器等。同时,以太网连接还提供了更稳定和快速的上网体验,特别是在下载大型文件或流媒体时。 总的来说,Android 13以太网功能为用户提供了更多连接选项,使得Android设备可以更好地适应不同的工作环境和网络条件。无论是在办公室、家庭还是移动工作中,以太网连接都可以提供更高效和可靠的网络体验。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值