【问题标题】:How to turn off Wifi via ADB?如何通过 ADB 关闭 Wifi?
【发布时间】:2012-04-19 11:56:09
【问题描述】:

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

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

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

到目前为止我已经找到了:

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

我只是不知道如何组合起来

【问题讨论】:

标签: android shell adb android-wifi


【解决方案1】:

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

启用:

adb shell su -c 'svc wifi enable'

禁用:

adb shell su -c 'svc wifi disable'

通过 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

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

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

关于“keyevents”here的更多信息。

【讨论】:

  • 非常感谢您的帖子。它在我的手机上不太一样,但是由于您发布了输入事件,我发现它是关键事件 19 和 23。再次感谢。 :)
  • 好的,你知道了吗?是的,我在 Droid X 上测试过,我不确定你的手机是什么 :)
  • 要在 Android 5.0 中使用这些键事件来切换 wifi,您可能不得不点击不同的事件然后回答。我会说adb shell input keyevent 19 & adb shell input keyevent 19 & adb shell input keyevent 23(第一个也可以是20)-如果您在进入wifi设置屏幕时没有选择任何选项(如果有,您应该跳过第一个事件)
  • 我在 2 年前回答了这个问题。这只是帮助某人自动执行任务的示例。
  • 这里需要su 就像@IvanMorgillo 说的adb shell su -c 'svc wifi disable'
【解决方案2】:

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

adb shell svc wifi enable|disable

【讨论】:

  • 我已经尝试在 6 种不同的设备上使用此代码,从 Gingerbread 到 JellyBean,有根设备到无根设备,但我无法让它在其中任何一个设备上运行。您可以在哪些设备上使用它?
  • @Nefarii - 为此我需要请求 adb shell 进入设备.....然后请求 su 权限....然后我能够正确运行这些命令。
  • 在根植的索尼爱​​立信 Xperia 和摩托罗拉 Razr 上工作
  • 在根 Nexus 5、4.4.2 上工作:adb shell su -c "svc wifi disable"
  • 这适用于我的三星 S8 无根,adb shell su -c 'svc wifi enable' 没有(因为它没有根)
【解决方案3】:

在非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:

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

ADB 命令:

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

【讨论】:

  • 我稍微调整了一下并更改了 BroadcastReceiver 的活动,因此它可以添加到任何应用程序中并且不会显示任何 GUI。
  • @ferdy182:能否请您发布代码以便其他人使用和评估它?
  • 这对我帮助很大。非常感谢!
  • 我们是否需要对 android N 进行任何更改?
【解决方案4】:

与引号一起使用

例如:adb shell "svc wifi enable"

这会起作用:)

【讨论】:

  • 我仍然无法让它工作,我已经在 Note、Note2、root GS3 和 Amaze 上尝试过。
  • 如果您在命令提示符下尝试此操作,它将起作用.. 它不会打开任何东西.. 该操作将在后台执行
【解决方案5】:
adb shell "svc wifi enable"

这有效且无需打开相关选项即可在后台执行操作!!

【讨论】:

    【解决方案6】:

    我测试了这个命令:

    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
    

    并且只能在窗口提示下工作,可能是因为某些驱动程序

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

    【讨论】:

    • 这特别有用:我的 Genymotion 模拟器随机启动时有可用的 wifi,但有时没有选择。我用这个:shell'am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings;输入keyevent 20;输入keyevent 23'
    【解决方案7】:
    1. 转到位置android/android-sdk/platform-tools
    2. shift+右击
    3. 在此处打开 cmd 并键入以下命令

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

    【讨论】:

      【解决方案8】:

      我只能这样做:

      settings put global wifi_on 0
      settings put global wifi_scan_always_enabled 0
      

      有时,如果在启动过程中完成(即修复bootloop such as this),它并不适用,您也可以先启用飞行模式:

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

      其他选项是通过以下方式强制执行此操作:

      while true; do settings put global wifi_on 0; done
      

      在 LG G5 (SE) 上的 Android 7 中使用(无根)stock mod 进行测试。

      【讨论】:

        【解决方案9】:

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

        不过,我终于找到了适合我的旧 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"
        

        【讨论】:

          【解决方案10】:

          这适用于 android 7.1.1,无根

          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
          

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

          【讨论】:

            【解决方案11】:

            以下方法不需要root并且应该可以在任何地方工作(根据docs,即使在Android Q+上,如果你保留targetSdkVersion = 28)。

            1. 制作一个空白应用。

            2. 创建ContentProvider

              class ApiProvider : ContentProvider() {
              
                  private val wifiManager: WifiManager? by lazy(LazyThreadSafetyMode.NONE) {
                      requireContext().getSystemService(WIFI_SERVICE) as WifiManager?
                  }
              
                  private fun requireContext() = checkNotNull(context)
              
                  private val matcher = UriMatcher(UriMatcher.NO_MATCH).apply {
                      addURI("wifi", "enable", 0)
                      addURI("wifi", "disable", 1)
                  }
              
                  override fun query(uri: Uri, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor? {
                      when (matcher.match(uri)) {
                          0 -> {
                              enforceAdb()
                              withClearCallingIdentity {
                                  wifiManager?.isWifiEnabled = true
                              }
                          }
                          1 -> {
                              enforceAdb()
                              withClearCallingIdentity {
                                  wifiManager?.isWifiEnabled = false
                              }
                          }
                      }
                      return null
                  }
              
                  private fun enforceAdb() {
                      val callingUid = Binder.getCallingUid()
                      if (callingUid != 2000 && callingUid != 0) {
                          throw SecurityException("Only shell or root allowed.")
                      }
                  }
              
                  private inline fun <T> withClearCallingIdentity(block: () -> T): T {
                      val token = Binder.clearCallingIdentity()
                      try {
                          return block()
                      } finally {
                          Binder.restoreCallingIdentity(token)
                      }
                  }
              
                  override fun onCreate(): Boolean = true
              
                  override fun insert(uri: Uri, values: ContentValues?): Uri? = null
              
                  override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<out String>?): Int = 0
              
                  override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0
              
                  override fun getType(uri: Uri): String? = null
              }
              
            3. AndroidManifest.xml 中声明它以及必要的许可:

              <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
              
              <application>
              
                  <provider
                      android:name=".ApiProvider"
                      android:authorities="wifi"
                      android:exported="true" />
              </application>
              
            4. 构建应用并安装它。

            5. 来自亚行的电话:

              adb shell content query --uri content://wifi/enable
              adb shell content query --uri content://wifi/disable
              
            6. 创建一个批处理脚本/shell 函数/shell 别名,使用短名称调用这些命令。

            根据您的设备,您可能需要额外的权限。

            【讨论】:

              【解决方案12】:

              ADB Connect to wifi with credentials :

              你可以使用下面的ADB命令连接wifi并输入密码:

              adb wait-for-device shell am start -n com.android.settingstest/.wifi.WifiSettings -e WIFI 1 -e AccessPointName "enter_user_name" -e Password "enter_password"

              【讨论】:

                【解决方案13】:

                在 Android 测试中,我这样做: InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("svc wifi enable") InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("svc wifi disable")

                【讨论】:

                  【解决方案14】:

                  在 Android 11 上通过 cmd 命令(无需 root)

                  启用 WIFI(开启):

                  adb shell cmd -w wifi set-wifi-enabled enable 
                  

                  禁用 WIFI(关闭):

                  adb shell cmd -w wifi set-wifi-enabled disable
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2016-12-09
                    • 2017-07-10
                    • 2015-10-14
                    • 2017-04-30
                    • 1970-01-01
                    • 2021-12-06
                    • 1970-01-01
                    相关资源
                    最近更新 更多