[MT8766][Android12] USB共享网络固定子网掩码跟网关并且默认开启USB共享网络

文章目录

    • 开发平台基本信息
    • 问题描述
    • 解决方法

开发平台基本信息

芯片: MT8766
版本: Android 12
kernel: msm-4.19

问题描述

客户要求设备默认开启usb共享网络,并且网络共享给PC端时,PC端动态获取到的子网掩码、网关是固定不变的。

解决方法

  • 解决方案与之前修改WIFI热点默认名称、密码、IP地址以及默认开启热点类似。

固定子网掩码跟网关

--- a/packages/modules/Connectivity/Tethering/src/android/net/ip/IpServer.java
+++ b/packages/modules/Connectivity/Tethering/src/android/net/ip/IpServer.java
@@ -112,6 +112,7 @@ public class IpServer extends StateMachine {
     // TODO: have PanService use some visible version of this constant
     private static final String BLUETOOTH_IFACE_ADDR = "192.168.44.1/24";
     private static final String WIFI_IFACE_ADDR = "192.168.68.68/24";
+    private static final String USB_IFACE_ADDR = "192.168.20.0/24";
 
     // TODO: have this configurable
     private static final int DHCP_LEASE_TIME_SECS = 3600;
@@ -660,6 +661,8 @@ public class IpServer extends StateMachine {
             return new LinkAddress(BLUETOOTH_IFACE_ADDR);
         }else if (mInterfaceType == TetheringManager.TETHERING_WIFI){
             return new LinkAddress(WIFI_IFACE_ADDR);
+        }else if (mInterfaceType == TetheringManager.TETHERING_USB){
+            return new LinkAddress(USB_IFACE_ADDR);
         }
 
         return mPrivateAddressCoordinator.requestDownstreamAddress(this, useLastAddress);

默认开启usb共享网络

 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
 import android.telephony.SubscriptionManager;
 import static android.net.ConnectivityManager.TETHERING_WIFI;
+import static android.net.ConnectivityManager.TETHERING_USB;
 import android.os.Handler;
 import android.os.Looper;
 import android.net.ConnectivityManager;
@@ -144,6 +145,9 @@ public class SecurityService extends ISecurityService.Stub {
                 ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
                 mConnectivityManager.startTethering(TETHERING_WIFI, true /* showProvisioningUi */,
                     mOnStartTetheringCallback, new Handler(Looper.getMainLooper()));
+                mConnectivityManager.startTethering(TETHERING_USB, true /* showProvisioningUi */,
+                    mOnStartTetheringCallback, new Handler(Looper.getMainLooper()));
+
 
                 Settings.Global.putInt(mContext.getContentResolver(),Settings.Global.ADB_ENABLED, 1);
 

你可能感兴趣的:(Android12,MTK,Android12,USB共享网络,固定子网掩码)