Created
February 28, 2017 09:03
-
-
Save acmi/cb8e24c3f343bc9090c1a86419a2e84a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.net.wifi.WifiManager; | |
import android.os.Handler; | |
import android.os.Looper; | |
import android.text.format.Formatter; | |
import android.util.Log; | |
import android.widget.Toast; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.Arrays; | |
import java.util.concurrent.Executor; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.atomic.AtomicBoolean; | |
public class AdbWiFi { | |
private static final String TAG = "ADB_WIFI"; | |
public static void enable(final Context context) { | |
final ExecutorService executor = Executors.newFixedThreadPool(4); | |
final AtomicBoolean error = new AtomicBoolean(); | |
executor.execute(() -> { | |
runCmd(new String[]{"setprop", "ro.secure", "0"}, executor, error); | |
runCmd(new String[]{"setprop", "ro.debuggable", "1"}, executor, error); | |
runCmd(new String[]{"setprop", "service.adb.tcp.port", "5555"}, executor, error); | |
runCmd(new String[]{"stop", "adbd"}, executor, error); | |
runCmd(new String[]{"start", "adbd"}, executor, error); | |
executor.shutdown(); | |
new Handler(Looper.getMainLooper()).post(() -> { | |
if (!error.get()) { | |
WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); | |
int ip = mWifiManager.getConnectionInfo().getIpAddress(); | |
//noinspection deprecation | |
Toast.makeText(context, "ADB WIFI " + Formatter.formatIpAddress(ip) + ":5555", Toast.LENGTH_SHORT).show(); | |
} else { | |
Toast.makeText(context, "ADB WIFI FAILED", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
}); | |
} | |
private static void runCmd(final String[] cmd, Executor executor, AtomicBoolean error) { | |
try { | |
final Process p = Runtime.getRuntime().exec(cmd); | |
executor.execute(() -> { | |
try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) { | |
String line; | |
while ((line = br.readLine()) != null) { | |
Log.i(TAG, line); | |
} | |
} catch (IOException ignore) { | |
} | |
}); | |
executor.execute(() -> { | |
try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()))) { | |
String line; | |
while ((line = br.readLine()) != null) { | |
Log.w(TAG, line); | |
} | |
} catch (IOException ignore) { | |
} | |
}); | |
if (p.waitFor() != 0) | |
error.set(true); | |
} catch (Exception e) { | |
Log.e(TAG, Arrays.toString(cmd), e); | |
error.set(true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment