简体   繁体   English

如何以编程方式在 Android 中启用自动 USB 网络共享或使用适用于 Android 7 及更高版本的 shell

[英]How to enable Auto USB Tethering in Android programmatically or using shell for Android 7 and higher

I've tried different ways to do it:我尝试了不同的方法来做到这一点:

  1. Tried Connectivity Manager class which is deprecated by Android because it doesn't have a setUsbTethering method.尝试过 Android 弃用的连接管理器类,因为它没有 setUsbTethering 方法。
  2. Tried this method:试过这个方法:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
public void switchOnTethering() {

    Object obj = getSystemService(Context.CONNECTIVITY_SERVICE);
    for (Method m : obj.getClass().getDeclaredMethods()) {

        if (m.getName().equals("tether")) {
            try {
                m.invoke(obj, "usb0");
            } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

  1. Tried adb shell service call connectivity 30,31,32,33,34 i32 1 command试过 adb shell service call connectivity 30,31,32,33,34 i32 1 命令
  2. Tried尝试过
adb shell
su
echo "rndis" > /sys/class/android_usb/android0/functions
echo "1" > /sys/class/android_usb/android0/enable
  1. Tried尝试过
adb shell
su
netcfg rndis0 up
netcfg rndis0 dhcp
  1. The only thing that works for me Adb shell svc usb setFunctions rndis but it works only in Android 11唯一对我有用的东西 Adb shell svc usb setFunctions rndis 但它只适用于 Android 11

I need to make it for Android 7 and higher.我需要为 Android 7 及更高版本制作它。 It's okay if there is a way needs root because I have rooted device.如果有办法需要 root 没关系,因为我有 root 设备。

In Android 7.0 it was adb shell svc usb setFunction rndisAndroid 7.0中是adb shell svc usb setFunction rndis

You can also try adb shell setprop sys.usb.config rndis您也可以尝试adb shell setprop sys.usb.config rndis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM