12

Im automating wifi off/on via adb.

I would either like to disable/enable wifi based on the test case

so far I have found good information here

However I want to test the connection before executing following 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

Above command disables wifi if enabled, enables wifi if disabled. I want to first test the connection and take action if required. I am wondering if there is a way to do using adb command. This can be achieved programatically but I want it to be done through adb for making it more reliable.

Also following command works only if device is rooted

adb shell "svc wifi enable" 

Also following command launches test on screen but provides no info via adb

adb shell am start -n com.android.settings/.wifi.WifiStatusTest
Community
  • 1
  • 1
Venkatesh
  • 3,558
  • 8
  • 32
  • 38

7 Answers7

19

To know everything about Wifi status of the device:

adb shell dumpsys wifi

To just know whether it's enabled or not:

adb shell dumpsys wifi | grep "Wi-Fi is"

To know more detailed status:

adb shell dumpsys wifi | grep "mNetworkInfo"
  1. If connected to a valid network, it shows "CONNECTED/CONNECTED". It also shows the name of the connected network.
  2. If wifi has been enabled but it is yet to connect to a network, it shows "DISCONNECTED/SCANNING".
  3. If wifi is disabled, it shows "DISCONNECTED/DISCONNECTED".
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Atish Sanyal
  • 199
  • 1
  • 3
  • Android O onwards. adb shell dumpsys wifi | grep "curState" will give following 3 otputs dpending upon state 1. "InitialState" : if Wifi is off. 2. "DisconnectedState" if wifi is on but disconnected , 3. "ConnectedState": if device connected to network. – Abhishek Nov 16 '18 at 10:09
  • Will this work, if I use those commands in Java, using Process and Runtime for example? – Keselme Dec 20 '18 at 08:33
  • adb shell dumpsys wifi | grep "mNetworkInfo" is not working with Android 11. do we have any alternatives? – Dhakshina Moorthy Jul 07 '21 at 08:43
15

Below command will give you the internet status and connectivity mode(cellular or Wi-Fi)

adb shell dumpsys connectivity
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
nramesh1990
  • 151
  • 1
  • 4
5

If you're trying to determine whether or not WiFi is turned on you can run adb shell settings get global wifi_on which returns 1 if it's on and 0 if it's off.

If you have multiple devices connected you can run adb -s [UDID_HERE] shell settings get global wifi_on to get the WiFi status of an individual phone. To find the UDID you can run adb devices.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
4

This would only tell you if there is internet access but you can do:

adb shell ping www.google.com
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Roberto Anić Banić
  • 1,411
  • 10
  • 21
3
adb shell dumpsys wifi | sed -n '1p'

is faster and safer than

adb shell dumpsys wifi | grep "Wi-Fi is"

because the output of dumpsys wifi contains the system logs that may have been contaminated by other applications

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
shenzhigang
  • 103
  • 8
  • The command line "dumpsys wifi" works for me. But be aware, in my case, I found on one android device the "Wi-Fi is enabled" is at the first line, while on another phone, such output is NOT at the first line, so I think `grep "Wi-Fi is"` is the better apporach. – MadHatter Jan 26 '22 at 08:14
0

Atish's answer was working fine until Android 11/R. For this OS onwards, I recommend using Abhishek's suggestion:

adb shell dumpsys wifi | grep "curState=ConnectedState"
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
-1

Not perfect, but you could dump UI layout and then search for ON/OFF text like this (bash):

adb pull $(adb shell uiautomator dump | grep -o '[^ ]*.xml') ui.xml
grep 'text="OFF"' ui.xml
if [ $? == 0 ]; then
  # Turn WIFI on
fi
kris
  • 1
  • 1