package jp.example.tethering; import android.content.Context; import android.os.Looper; import java.lang.reflect.Method; /** * Runs as a separate root process via: * * CLASSPATH= /system/bin/app_process /system/bin \ * jp.example.tethering.TetherCli usb true * * A normal app cannot call setUsbTethering() (it needs TETHER_PRIVILEGED), * but uid 0 can. */ public class TetherCli { public static void main(String[] args) { String mode = args.length > 0 ? args[0] : "usb"; boolean enable = args.length < 2 || "true".equalsIgnoreCase(args[1]); try { Looper.prepareMainLooper(); Class at = Class.forName("android.app.ActivityThread"); Object thread = at.getMethod("systemMain").invoke(null); Context ctx = (Context) at.getMethod("getSystemContext").invoke(thread); if ("usb".equals(mode)) { Object cm = ctx.getSystemService(Context.CONNECTIVITY_SERVICE); Method m = cm.getClass().getMethod("setUsbTethering", boolean.class); m.invoke(cm, enable); System.out.println("OK usb " + enable); } else if ("wifi".equals(mode)) { Object wm = ctx.getSystemService(Context.WIFI_SERVICE); Method m = wm.getClass().getMethod("setWifiApEnabled", android.net.wifi.WifiConfiguration.class, boolean.class); m.invoke(wm, null, enable); System.out.println("OK wifi " + enable); } else if ("bt".equals(mode)) { Object cm = ctx.getSystemService(Context.CONNECTIVITY_SERVICE); Method m = cm.getClass().getMethod("setBluetoothTethering", boolean.class); m.invoke(cm, enable); System.out.println("OK bt " + enable); } else { System.out.println("unknown mode " + mode); } } catch (Throwable t) { t.printStackTrace(); } try { Thread.sleep(1200); } catch (InterruptedException ignored) { } } }