How to switch(not only on or off) the mobile data and wifi using adb? 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 &quot;adb shell svc data toggle&quot;. 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 &quot;on&quot; or &quot;off&quot;.
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.
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.