3

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.

2

By knowing the state I suppose you meant whether at X point of time the Wi-Fi or mobile data is enabled or disabled. This information can be obtained from the dump of connectivity service.

Run this command to get the status of mobile data and Wi-Fi:

adb shell dumpsys connectivity | sed -e '/[0-9] NetworkAgentInfo.*CONNECTED/p' -e '/Tether state:/,/Hardware offload:/p' -n

Example output:

  1 NetworkAgentInfo [WIFI () - 178] CONNECTED/CONNECTED
  5 NetworkAgentInfo [MOBILE (LTE) - 191] CONNECTED/CONNECTED
Tether state:
  rndis0 - TetheredState - lastError = 0
  wlan0 - AvailableState - lastError = 0
  Upstream wanted: true
  Current upstream interface(s): [rmnet_data1]
Hardware offload:

Here:

  • WIFI means device is connected to a Wi-FI
  • MOBILE means mobile data is enabled
  • rndis is interface name of USB tethering
  • wlan is interface name of Wi-Fi hotspot
  • TetheredState means tethering for the respective interface is enabled

In your output:

  • if WIFI term with CONNECTED status is missing, than device is not connected to a Wi-FI
  • if MOBILE term with CONNECTED status is missing, than mobile device is disabled
  • if TetheredState is shown for rndis or wlan or both, than respectively, it means USB tethering and Wi-Fi hotspot is/are enabled.

To check whether Wi-Fi radio is enabled or disabled, run this command:

adb shell dumpsys wifi | sed -n 1p

Example output if Wi-Fi is enabled:

Wi-Fi is enabled

enabled would shows as disabled if Wi-Fi radio is disabled.


Similarly, to check if cellular radio has telecom network access, or if it is disabled, run this command:

adb shell dumpsys telephony.registry | grep mServiceState | grep -o "mDataRegState=[()0-9A-Z_]*"

Example output:

mDataRegState=0(IN_SERVICE)
mDataRegState=0(IN_SERVICE)

Here:

  • IN_SERVICE means your cellular radio or radios (if multiple SIM cards are supported) is connected to a network.
  • OUT_OF_SERVICE would mean not connected to telecom's network
  • POWER_OFF would mean device is in Airplane mode.

Combining all these information would help your fully ascertain:

  • the state of Wi-Fi radio and its connectivity
  • the state of cellular radio and its internet connectivity
  • the state of USB and Wi-Fi tethering

Now, you already know how to enable/disable mobile data, USB tethering, and Wi-Fi using svc utility. While I don't know how to easily turn on hotspot, disabling the hotspot is pretty easy. First, enable the Wi-Fi using svc, and then disable it with svc too.

The aforesaid two commands should be sufficient to ascertain the connected status of mobile data, Wi-Fi, Wi-Fi hotspot, and USB tethering.

Note: this solution was tested on a OnePlus 6 running Android 10.

| improve this answer | |
2

Mobile data:

adb shell get global mobile_data 
adb shell get global mobile_data_always_on

Wifi:

adb shell get global wifi_on

Airplane mode:

adb shell get global airplane_mode_on
| improve this answer | |
  • thank. this is what I need. a couple of days ago I found these commands and will finish the bash script soon. I'll try not to forget to add it as an answer to my question when I finish it. in the script i use these adb commands: adb shell settings get global adb shell settings put global adb shell am broadcast adb shell svc it allows full control of wifi, cellular, m-data, bluetooth. I just have to figure out how to turn usb-modem and wifi-hotspot on and off via adb. and there are problems with auto-completion of bash script commands as you type, but that's a different topic. – NikolayFR Aug 16 at 22:12
1

get state:

adb shell settings get global airplane_mode_on
# 0|1 -  dis/en abled

adb shell settings get global mobile_data
# 0|1 - dis/en abled

adb shell settings get global wifi_on
# 0|1 - dis/en abled

adb shell settings get global bluetooth_on
# 2|1 - dis/en abled

adb shell settings get global cell_on
# 2|1 - dis/en abled

change state:

airplane
  enable
    adb shell settings put global airplane_mode_on 1
    adb shell am broadcast -a android.intent.action.AIRPLANE_MODE
  disable
    adb shell settings put global airplane_mode_on 0
    adb shell am broadcast -a android.intent.action.AIRPLANE_MODE

mobile data
    adb shell svc data enable
    adb shell svc data disable

wifi
    adb shell svc wifi enable
    adb shell svc wifi disable

bluetooth
  eanable
    adb shell settings put global bluetooth_disabled_profiles 1
  disable 
    adb shell settings put global bluetooth_disabled_profiles 0

to toggle use bash script that checks state and then switch on/off like:

#!/bin/bash
function toggle_data {
    if [[ $(adb shell settings get global mobile_data) == "1" ]]; then
        adb shell svc data disable
    else
        adb shell svc data enable; fi
}
function toggle_wifi {
    if [[ $(adb shell settings get global wifi_on) == "1" ]]; then
        adb shell svc wifi disable
    else
        adb shell svc wifi enable; fi
}
toggle_wifi
toggle_data
exit 0;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.