The following is non-root method:
- Create a dummy app in Android Studio. [Credit]
MainActivity.java, put right after setContentView(R.layout.activity_main)
:
WifiManager wfm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
try {
wfm.setWifiEnabled(Boolean.parseBoolean(getIntent().getStringExtra("wifi")));
}
catch (Exception e) {
}
System.exit(0);
AndroidManifest.xml, put before <application
:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
- Enabled USB debugging settings, plug USB cable to your phone, press Shift+F10 to run the app which will also install the app.
- In your phone, navigate to Advanced Wi-Fi and get the IPv4 address.
- While USB cable still plugged, issue
adb shell
command,
- Issue
pm list packages
command to find out the package name if not sure. For example package:com.blogspot.diannaoxiaobai.dummyonly, remove the prefix package: is your package name. Or you can simply get the package name in AndroidManifest.xml, e.g. package="com.blogspot.diannaoxiaobai.dummyonly"
.
- Issue
setprop persist.adb.tcp.port 5555
(credit) command.
- Ctrl+D to quit adb session.
- Unplug your cable. Reboot your phone now.
- Assume your phone automatically connected to wifi on reboot, and your laptop connected to the same wifi network. No need any USB cable now.
- Issue
adb connect 192.168.11.5
, in which this ip get from step #2 above.
- Issue
adb shell am start -n com.blogspot.diannaoxiaobai.dummyonly/.MainActivity -e wifi false
to disable wifi. com.blogspot.diannaoxiaobai.dummyonly is the package name get from step #4 above.
- You can remove the port 5555 set from step #5 above with
adb shell setprop persist.adb.tcp.port ""
command. (Credit)