13

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

7 Answers 7

20

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".
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Will this work, if I use those commands in Java, using Process and Runtime for example?
adb shell dumpsys wifi | grep "mNetworkInfo" is not working with Android 11. do we have any alternatives?
15

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

adb shell dumpsys connectivity

Comments

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.

Comments

4

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

adb shell ping www.google.com

4 Comments

But this does not work on my Sony Xperia device. This command gives no information.
don't you get something after typing this?
Nope. However I am able to connect to internet on device
Well, I'm sorry it doesn't work, I'll try to find sth else
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

1 Comment

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.
1

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"

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.