Android 如何通过 ADB 关闭 Wifi?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10033757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:16:38  来源:igfitidea点击:

How to turn off Wifi via ADB?

androidshelladbandroid-wifi

提问by Nefariis

Im automating a testing procedure for wifi calling and I was wondering is there a way to turn off/on wifi via adb?

我正在自动执行 wifi 调用的测试程序,我想知道有没有办法通过 adb 关闭/打开 wifi?

I would either like to disable/enable wifi or kill wifi calling (com.movial.wificall) and restart it.

我想禁用/启用 wifi 或终止 wifi 呼叫(com.movi​​al.wificall)并重新启动它。

Is it possible to do this all via adb and shell commands?

是否可以通过 adb 和 shell 命令来完成这一切?

so far I have found:

到目前为止,我发现:

android.net.wifi.WifiManager
setWifiEnabled(true/false)

Im just not sure how to put it together

我只是不知道如何把它放在一起

回答by Jared Burrows

Using "svc" through ADB (rooted required):

通过 ADB 使用“svc”(需要 root):

Enable:

使能够:

adb shell su -c 'svc wifi enable'

Disable:

禁用:

adb shell su -c 'svc wifi disable'

Using Key Events through ADB:

通过 ADB 使用关键事件:

adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings
adb shell input keyevent 20 & adb shell input keyevent 23

The first line launch "wifi.WifiSettings" activity which open the WiFi Settings page. The second line simulate key presses.

第一行启动“wifi.WifiSettings”活动,打开 WiFi 设置页面。第二行模拟按键操作。

I tested those two lines on a Droid X. But Key Events above probably need to edit in other devices because of different Settings layout.

我在 Droid X 上测试了这两行。但是由于不同的设置布局,上面的关键事件可能需要在其他设备中进行编辑。

More info about "keyevents" here.

更多关于“keyevents”的信息在这里

回答by Berkk

I was searching for the same to turn bluetooth on/off, and I found this:

我正在寻找相同的方法来打开/关闭蓝牙,我发现了这个:

adb shell svc wifi enable|disable

回答by Ruslan

Simple way to switch wifi on non-rooted devices is to use simple app:

在非root设备上切换wifi的简单方法是使用简单的应用程序:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        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:

AndroidManifest.xml:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

ADB commands:

亚行命令:

$ adb shell am start -n org.mytools.config/.MainActivity -e wifi true
$ adb shell am start -n org.mytools.config/.MainActivity -e wifi false

回答by Musab

  1. go to location android/android-sdk/platform-tools
  2. shift+right click
  3. open cmd here and type the following commands

    1. adb shell
    2. su
    3. svc wifi enable/disable
  4. done!!!!!

  1. 前往地点 android/android-sdk/platform-tools
  2. shift+右键单击
  3. 在这里打开cmd并输入以下命令

    1. adb shell
    2. su
    3. svc wifi enable/disable
  4. 完毕!!!!!

回答by Arun

adb shell "svc wifi enable"

This worked & it makes action in background without opening related option !!

这行得通,并且无需打开相关选项即可在后台执行操作!

回答by Igor Maia Romcy

I tested this command:

我测试了这个命令:

adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings
adb shell input keyevent 19 && adb shell input keyevent 19 && adb shell input keyevent 23

and only works on window's prompt, maybe because of some driver

并且只适用于窗口的提示,可能是因为某些驱动程序

The adb shell svc wifi enable|disablesolution only works with root permissions.

adb shell svc wifi enable|disable解决方案仅适用于 root 权限。

回答by Trevi?o

I can just do:

我只能这样做:

settings put global wifi_on 0
settings put global wifi_scan_always_enabled 0

Sometimes, if done during boot (i.e. to fix bootloop such as this), it doesn't apply well and you can proceed also enabling airplane mode first:

有时,如果在启动期间完成(即修复bootloop 之类的),它并不适用,您还可以继续先启用飞行模式:

settings put global airplane_mode_on 1
settings put global wifi_on 0
settings put global wifi_scan_always_enabled 0

Other option is to force this with:

其他选项是强制执行此操作:

while true; do settings put global wifi_on 0; done

Tested in Android 7 on LG G5 (SE) with (unrooted) stock mod.

在 LG G5 (SE) 上的 Android 7 中使用(无根)股票模组进行测试。

回答by SebMa

All these input keyeventcombinations are SO android/hardware dependent, it's a pain.

所有这些input keyevent组合都依赖于安卓/硬件,这很痛苦。

However, I finally found the combination for my old android 4.1 device :

但是,我终于找到了适用于我的旧 android 4.1 设备的组合:

adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings
adb shell "input keyevent KEYCODE_DPAD_LEFT;input keyevent KEYCODE_DPAD_RIGHT;input keyevent KEYCODE_DPAD_CENTER"

回答by Adam Deane

This works as of android 7.1.1, non-rooted

这适用于 android 7.1.1,非 root

adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings && adb shell input keyevent 23 && adb shell input keyevent 23

This will launch the activity and then send the DPAD center keyevent. I added another DPAD center as an experiment to see if you can enable it in the same way.

这将启动活动,然后发送 DPAD 中心关键事件。我添加了另一个 DPAD 中心作为实验,看看您是否可以以相同的方式启用它。

回答by SanalBathery

use with quotes

与引号一起使用

ex: adb shell "svc wifi enable"

前任: adb shell "svc wifi enable"

this will work :)

这将工作:)