summaryrefslogtreecommitdiff
path: root/src/jp/example/tethering/TetherCli.java
diff options
context:
space:
mode:
authorroot <root@dangofactory.local>2026-11-23 10:20:49 +0900
committerroot <root@dangofactory.local>2026-11-23 10:20:49 +0900
commita67bdcc65a165d1da57ae6d6fead84a1d2e5b248 (patch)
treed0075dacac62d509a09974c1cd2f18ca72cf0e39 /src/jp/example/tethering/TetherCli.java
downloadtethering-app-main.tar.gz
tethering-app-main.zip
Import tethering-appHEADmain
Diffstat (limited to 'src/jp/example/tethering/TetherCli.java')
-rw-r--r--src/jp/example/tethering/TetherCli.java54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/jp/example/tethering/TetherCli.java b/src/jp/example/tethering/TetherCli.java
new file mode 100644
index 0000000..4cf3336
--- /dev/null
+++ b/src/jp/example/tethering/TetherCli.java
@@ -0,0 +1,54 @@
+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=<this apk> /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) {
+ }
+ }
+}