Adb turn off wifi

How to turn off Wifi via ADB?

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 wifi calling (com.movial.wificall) and restart it.

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

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

Im just not sure how to put it together

Android Solutions

Solution 1 — Android

#Using «svc» through ADB (rooted required):

adb shell su -c 'svc wifi enable' 
adb shell su -c 'svc wifi disable' 

#Using Key Events through 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.

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.

More info about «keyevents» here.

Solution 2 — Android

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

adb shell svc wifi enable|disable 

Solution 3 — Android

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

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); > > 
name="android.permission.CHANGE_WIFI_STATE" /> 
$ adb shell am start -n org.mytools.config/.MainActivity -e wifi true $ adb shell am start -n org.mytools.config/.MainActivity -e wifi false 

Solution 4 — Android

  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

    Solution 5 — Android

    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.

    Solution 6 — Android

    It will work if you use with quotes:

    adb shell "svc wifi enable" 

    Solution 7 — Android

    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.

    Solution 8 — Android

    Via cmd command on Android 11 & 12 (no-root-required)
    adb shell cmd -w wifi set-wifi-enabled enabled 
    adb shell cmd -w wifi set-wifi-enabled disabled 

    Solution 9 — Android

    All these input keyevent combinations are SO android/hardware dependent, it’s a pain.

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

    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" 

    Solution 10 — Android

    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.

    Solution 11 — Android

    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: Arrayout String>?, selection: String?, selectionArgs: Arrayout 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 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: Arrayout String>?): Int = 0 override fun delete(uri: Uri, selection: String?, selectionArgs: Arrayout String>?): Int = 0 override fun getType(uri: Uri): String? = null > 
    name="android.permission.CHANGE_WIFI_STATE" /> name=".ApiProvider" android:authorities="wifi" android:exported="true" /> 
    adb shell content query --uri content://wifi/enable adb shell content query --uri content://wifi/disable 

    Depending on your device you may need additional permissions.

    Solution 12 — Android

    ADB Connect to wifi with credentials :

    You can use the following ADB command to connect to wifi and enter password as well :

    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»

    Solution 13 — Android

    In Android tests I’m doing as follows:

    InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("svc wifi **enable**") 
    InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("svc wifi **disable**") 

    Источник

    How to turn off wifi via adb in Android?

    If you are developing an Android application or are troubleshooting issues on an Android device, you may need to turn off the Wi-Fi connection from time to time. This can be accomplished using ADB (Android Debug Bridge) commands, which are used to communicate with an Android device from a computer. In this article, we will discuss two methods for turning off Wi-Fi on an Android device using ADB commands.

    Method 1: Using adb shell commands

    To turn off Wifi via ADB using adb shell commands, follow these steps:

    1. Open your terminal or command prompt and navigate to the directory where your adb executable is located.
    2. Connect your Android device to your computer via USB cable.
    3. Type the following command to open a shell on your device:

    This command will turn off Wifi on your device.

    This command will turn on Wifi on your device.

    That’s it! You have successfully turned off Wifi on your Android device using adb shell commands.

    Method 2: Using adb shell am commands

    To turn off the Wifi on an Android device via ADB using the am (Activity Manager) commands, follow the steps below:

    1. Connect your Android device to your computer via USB cable and enable USB debugging in the Developer Options on your device.
    2. Open a terminal or command prompt window on your computer and type the following command to check if your device is connected:
    adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiPickerActivity
    adb shell input tap 600 200
    adb shell dumpsys wifi | grep Wi-Fi\ Enabled

    That’s it! You have successfully turned off the Wifi on your Android device via ADB using the am commands.

    Источник

    How to switch(not only on or off) the mobile data and wifi using adb?

    First main question: is it possible to switch(not only on or off) the state of the mobile data and wifi using adb? (for example for wifi: if you don’t know the state of the wifi now. you send several commands and then you are sure that the wifi state changed.) if this is not possible then the question is how to find out the state of wifi and mobile data using adb? second: is it possible to switch the state of the wifi-hotspot and the usb-modem via abd? what can I do: I’m use

    adb shell svc data enable/disable adb shell svc wifi enable/disable 

    To turn on/of wifi and mobile data. But I don’t know a way to switch the state with one command like «adb shell svc data toggle». Maybe there is a command that returns the state of the Wi-Fi or mobile data? Like adb shell svc data state . and it returns «on» or «off». is there a way to switch it when the phone is locked? I also use such commands to switch the wifi-hotspot and usb-modem (unlike first two commands above the screen may be off, but the phone must be unlocked):

    adb shell am start -n com.android.settings/.TetherSettings adb shell input keyevent 20 adb shell input keyevent 20 adb shell input keyevent 20 adb shell input keyevent 66 

    first line opens the settings page. keyevent 20 — simulates key press down , keyevent 66 — enter . it’s for usb-modem. for wifi-hotspot i use same first line,key20 key66 key20 key66. i have samsung a50(android 10) and ubuntu 20. the modem state switching bash script is located at /usr/local/bin so I can run it from the console just by writing the filename.

    Источник

    Using Key Events through ADB:

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

    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.

    More info about «keyevents» here.

    Solution 2

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

    adb shell svc wifi enable|disable 

    Solution 3

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

    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); > > 
    $ adb shell am start -n org.mytools.config/.MainActivity -e wifi true $ adb shell am start -n org.mytools.config/.MainActivity -e wifi false 

    Solution 4

    adb shell "svc wifi enable" 

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

    Solution 5

    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

      Nefariis

      Nefariis

      Pragmatic developer and data enthusiast — I specialize in all things automation. I write in Python, Kotlin, JAVA, Typescript, Regex, and SQL to create custom applications that support the teams around me. For fun I wrestle alligators, battle rap in NY subways, and Netflix and chill with Tony Danza. Achievements: I’ve seen the internet in its entirety, my record is 14 marshmallows in chubby bunny, and — I know a girl.

      Comments

      Nefariis

      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 wifi calling (com.movial.wificall) and restart it. Is it possible to do this all via adb and shell commands? so far I have found:

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

      Nefariis

      Thank you very much for the post. Its not quite the same on my phone, but since you posted the input events I figured out it is key events 19 and 23. Thanks Again. : )

      Jared Burrows

      Nefariis

      I’ve tried using this code on 6 different devices ranging from Gingerbread to JellyBean, rooted to non-rooted and I couldn’t get it to work on any of them. What devices are you able to get this to work on?

      @Nefarii — for this I needed to request adb shell into the device. then request su permission. then I was able to run these commands properly.

      I tweaked this a little bit and changed the activity for a BroadcastReceiver so it can be added to any app and It won’t show any GUI.

      Источник

      Читайте также:  Увеличить скорость wifi домашней сети
Оцените статью
Adblock
detector