How to turn off Wifi via ADB?

前端 未结 13 2160
北荒
北荒 2020-11-29 17:37

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

I would either like to disable/enable wifi or kill

相关标签:
13条回答
  • 2020-11-29 18:08

    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|disable solution only works with root permissions.

    0 讨论(0)
  • 2020-11-29 18:10
    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!!!!!

    0 讨论(0)
  • 2020-11-29 18:12

    This works as of android 7.1.1, non-rooted

    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.

    0 讨论(0)
  • 2020-11-29 18:15

    The following method doesn't require root and should work anywhere (according to docs, even on Android Q+, if you keep targetSdkVersion = 28).

    1. Make a blank app.

    2. Create a 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. Declare it in AndroidManifest.xml along with necessary permission:

      <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
      
      <application>
      
          <provider
              android:name=".ApiProvider"
              android:authorities="wifi"
              android:exported="true" />
      </application>
      
    4. Build the app and install it.

    5. Call from ADB:

      adb shell content query --uri content://wifi/enable
      adb shell content query --uri content://wifi/disable
      
    6. Make a batch script/shell function/shell alias with a short name that calls these commands.

    Depending on your device you may need additional permissions.

    0 讨论(0)
  • 2020-11-29 18:17

    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:

    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.

    0 讨论(0)
  • 2020-11-29 18:21
    adb shell "svc wifi enable"
    

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

    0 讨论(0)
提交回复
热议问题