How to turn off Wifi via ADB?

前端 未结 13 2218
北荒
北荒 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: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?, selection: String?, selectionArgs: Array?, 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  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?): Int = 0
      
          override fun delete(uri: Uri, selection: String?, selectionArgs: Array?): Int = 0
      
          override fun getType(uri: Uri): String? = null
      }
      
    3. Declare it in AndroidManifest.xml along with necessary permission:

      
      
      
      
          
      
      
    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.

提交回复
热议问题