How to test wifi connection via adb?

33,920

Solution 1

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

adb shell dumpsys connectivity

Solution 2

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

Solution 3

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.

Solution 4

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

adb shell ping www.google.com

Solution 5

adb shell dumpsys wifi | sed -n '1p'

is faster and more safely than

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

Because the output of 'dumpsys wifi' contains the system logs that may has been contaminated by other application

Share:
33,920
Venkatesh
Author by

Venkatesh

Updated on July 09, 2022

Comments

  • Venkatesh
    Venkatesh almost 2 years

    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
    
  • Venkatesh
    Venkatesh almost 9 years
    But this does not work on my Sony Xperia device. This command gives no information.
  • Roberto Anić Banić
    Roberto Anić Banić almost 9 years
    don't you get something after typing this?
  • Venkatesh
    Venkatesh almost 9 years
    Nope. However I am able to connect to internet on device
  • Roberto Anić Banić
    Roberto Anić Banić almost 9 years
    Well, I'm sorry it doesn't work, I'll try to find sth else
  • Abhishek
    Abhishek over 5 years
    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.
  • Keselme
    Keselme over 5 years
    Will this work, if I use those commands in Java, using Process and Runtime for example?
  • Dhakshina Moorthy
    Dhakshina Moorthy almost 3 years
    adb shell dumpsys wifi | grep "mNetworkInfo" is not working with Android 11. do we have any alternatives?
  • MadHatter
    MadHatter over 2 years
    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.