How to turn off Wifi via ADB?

Go To StackoverFlow.com

45

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)

Im just not sure how to put it together

2012-04-05 18:11
by Nefariis
https://gist.github.com/kamleshkarwande/92a2421ee3f1881ed10d59949b6a4e4 - Kamlesh Karwande 2018-05-29 14:49


89

Using "svc" through ADB (rooted required):

Enable:

adb shell su -c 'svc wifi enable'

Disable:

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.

2012-04-06 02:48
by Jared Burrows
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. : - Nefariis 2012-04-10 15:56
Ok you got it? Yeah I tested that on a Droid X, I wasn't sure what phone you had : - Jared Burrows 2012-04-10 16:21
In my opinion answer by Berkk is much better (notably less hacky) - karni 2013-04-11 19:44
to use those keyevents to toggle wifi in Android 5.0 you would probably have to hit different events then in answer. I would say adb shell input keyevent 19 & adb shell input keyevent 19 & adb shell input keyevent 23 (the first one can be 20 as well) - this is working if there is no option selected when you enter the wifi settings screen (if there is, you should skip the first event - Bartek Lipinski 2015-01-22 23:55
I answered this over 2 years ago. It was just an example to help someone automate a task - Jared Burrows 2015-01-23 00:36
In order to get this to work I had to run 'adb root' first - McP 2015-04-13 12:01
@McP Which one - Jared Burrows 2015-04-13 15:39
adb shell svc wifi enable/disable. Also just realised there where several comments about this in Berkk's answer, my bad - McP 2015-04-13 23:14
svc wifi enable/disable works only for rooted devices. The other way of automating by using keyevent won't work across all androids - Thejus Krishna 2015-09-04 10:46
@ThejusKrishna Why did you downvote this - Jared Burrows 2015-09-05 03:54
@ThejusKrishna See the date above. I posted this answer over 3 years ago with minor edits. If you want to make an edit you can. Please learn how to use Stack Overflow - Jared Burrows 2015-09-06 15:34
Please don't feel offended. This answer makes no sense across all the android phones. Better answer was found below and definitely feel this is outdated - Thejus Krishna 2015-09-07 05:48
it required su here like @IvanMorgillo said adb shell su -c 'svc wifi disable'Aquarius Power 2016-02-18 02:24
i tried to disable the wifi and got: sh: resetreason: can't execute: Permission denied how should i get permissions? the phone wasn't locked eithe - ThunderWiring 2017-04-19 20:45


43

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

adb shell svc wifi enable|disable
2012-07-13 14:41
by Berkk
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 - Nefariis 2012-09-11 21:44
@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 - dell116 2012-10-18 20:47
Ensure you have root access for this to work - muslidrikk 2013-02-05 11:12
I can confirm that this works on my rooted Nexus 7 - Lennart 2013-02-19 15:50
I confirm this also works on rooted Nexus 4 - karni 2013-04-11 19:43
Works on rooted Sony Ericsson Xperia and Motorola Raz - Ari 2013-09-20 13:18
Working on rooted Nexus 5, 4.4.2: adb shell su -c "svc wifi disable - Ivan Morgillo 2014-05-20 09:22
this works on my Samsung S8 non-rooted, the adb shell su -c 'svc wifi enable' didn't (since it wasn't rooted - trevren11 2018-06-15 16:49


24

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);
    }
}

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
2012-09-14 08:18
by Ruslan
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 - Fernando Gallego 2012-11-05 15:40
@ferdy182: Can you please post the code so that others may use and evaluate it - GodMan 2012-11-07 09:46
@GodMan find it here https://plus.google.com/110502863648146989289/posts/Ri1Dcm6cn4 - Fernando Gallego 2012-12-20 09:45
This helped me very much. Thanks a lot - Esenbek Kydyr uulu 2015-05-28 11:56
Do we need to make any change for android N - Thejus Krishna 2017-07-21 04:45
i used below command on samsung s5 , but it has not enabled any wifi adb -s 4d0075754fdb41cd shell am start -n com.android.settings/.wifi.WifiSettings wifi Tru - pankaj mishra 2018-02-23 11:26


4

  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!!!!!

2015-10-29 08:38
by Musab


3

adb shell "svc wifi enable"

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

2013-05-09 05:29
by Arun


2

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

about adb shell svc wifi enable|disable i guess that only work with root permission

2015-06-08 14:26
by Igor Maia Romcy
This was particularly useful: my Genymotion emulators randomly start up with the wifi available but sometimes not selected. I used this: shell 'am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings; input keyevent 20; input keyevent 23 - android.weasel 2016-12-08 15:16


0

use with quotes

ex: adb shell "svc wifi enable"

this will work :)

2013-01-24 14:02
by SanalBathery
I still cant get this too work, Ive tried it on the Note, Note2, a rooted GS3, and an Amaze - Nefariis 2013-01-28 23:04
if you try this in a command prompt it will work.. It will not open anything.. the action will be performed in backgroun - SanalBathery 2014-03-21 11:49


0

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.

2017-11-21 18:49
by Treviño
Ads