How to disconnect from a wifi network through adb? - android

I understand we could turn off Wifi through the "adb shell svc wifi disable" command but I don't want to completely turn off Wifi. I want to disconnect from a particular ssid through adb. Is it possible?
Edit: I got a notification saying this question is identified as a dupe of this question: How to turn off Wifi via ADB?. It actually is not. As mentioned clearly in the first paragraph, I don't want to turn off wifi but want to simply disconnect from a particular network. This is like long pressing a network and tapping "Forget network". Essentially I want to simulate a condition of the user moving out of a wifi network without having to turn off wifi.

If you have root access, you can make it by using wpa_cli.
First, you can use the wpa_cli`s list_networks to get the network id of the network that you want to disconnect
$ adb shell
# wpa_cli
> list_networks
**network id** / ssid / bssid / flags
0 001aLinksys14081-2G any [CURRENT]
and then you just need to run the wpa_cli remove_network {network id}, where the {network id} parameter is the one that you got on the list_networks method.

One option is to use cmd wifi. For disconnecting from a Wi-Fi network, you can "forget" it like so:
adb shell cmd wifi list-networks
adb shell cmd wifi forget-network <networkId from list-networks>
You can view the help documentation for cmd wifi with:
adb shell cmd wifi -h

This command did it for me
docker exec -it $container_id /bin/bash -c "cd /root//shared-tools/android-sdk/platform-tools; ./adb shell 'svc wifi disable'"

You cannot. But, this will not be enough to satisfying your question, there is tricky way to connect to "other wifi". (Yes, it is not disconnect)
Install an app, and send command to the app via adb.
See this: https://stackoverflow.com/a/37303412/850347
I already build an app which does so and it's available here: https://github.com/steinwurf/adb-join-wifi
Once the app is installed, a wifi access point can be joined using the following ADB command:
adb shell am start -n com.steinwurf.adbjoinwifi/com.steinwurf.adbjoinwifi.MainActivity -e ssid [SSID] -e password_type [PASSWORD_TYPE] -e password [WIFI PASSWORD]
See this: https://stackoverflow.com/a/37303412/850347

Related

Android device: How to connect wifi by "Adb shell cmd wifi" command?

Android device: How to connect wifi by "Adb shell cmd wifi" command?
I tried to connect wifi by adb to avoid tedious click and verification operations by appium. And i found that the "adb shell cmd wifi" have a keyword "connect-network", but i cant connect the wifi by this way. Does anyone know how to use it? Thank a lot guys!
Here is the usage of the "Adb shell cmd wifi connect-network":Wi-Fi (wifi) commands:
connect-network <ssid> open|owe|wpa2|wpa3 [<passphrase>] [-m] [-d] [-b <bssid>]
Connect to a network with provided params and add to saved networks list
<ssid> - SSID of the network
open|owe|wpa2|wpa3 - Security type of the network.
- Use 'open' or 'owe' for networks with no passphrase
- 'open' - Open networks (Most prevalent)
- 'owe' - Enhanced open networks
- Use 'wpa2' or 'wpa3' for networks with passphrase
- 'wpa2' - WPA-2 PSK networks (Most prevalent)
- 'wpa3' - WPA-3 PSK networks
-m - Mark the network metered.
-d - Mark the network autojoin disabled.
-b <bssid> - Set specific BSSID.
And my input is adb shell cmd wifi connect-network WIFIName open wpa2 Loginpassword, it return me a Connection initiated, But the connect is not successful. Why this happen? Should i input the wifi login account? And what the whole command should be?
Hey I think that you should use either 'open' or 'wpa2' not both. But If we assume that wifi is secured by a wpa2 password so proper command would be:
adb shell cmd wifi connect-network WIFIName wpa2 password
Also, take into consideration that only the root user can invoke wifi commands, and root access is not available in most cases.

Activate ADB wireless without USB connection to the device

I found several isntructions to activate ADB over wireless network instead USB connection (like this) but I have a problem: my device has the USB port broken.
I cannot execute adb tcpip 5555 because that, so I need to activate ADB over wireless if I want to use ADB to control the device.
Is there any way to do that without rooting the device? I tried several commands on a terminal emulator, but I didn't accomplish my objective.
Any proposal on that?
I have managed to do this using ssh with the following script. It might require some small adjustments (our platform is using systemctl, for one, so you might need to change those calls).
#/bin/bash
if ! command -v sshpass &> /dev/null
then
echo "sshpass could not be found. Please install it by running 'sudo apt install sshpass'"
exit
fi
IP="192.168.0.111"
USERNAME="user"
PASSWORD="password"
if [ $1 -a $1 == "disable" ]
then
read -n 1 -s -r -p "To disable adb over WiFi please plug in the USB cable and press any key."
adb disconnect $IP
adb kill-server
sshpass -p $PASSWORD ssh -o StrictHostKeyChecking=no $USERNAME#$IP "setprop service.adb.tcp.port -1; systemctl stop adbd; killall adbd &>/dev/null; systemctl start adbd &>/dev/null"
else
read -n 1 -s -r -p "To enable adb over WiFi please unplug USB cable and press any key."
adb kill-server &>/dev/null
sshpass -p $PASSWORD ssh -o StrictHostKeyChecking=no $USERNAME#$IP "setprop service.adb.tcp.port 5555; systemctl stop adbd; killall adbd; systemctl start adbd &>/dev/null"
adb kill- &>/dev/null
adb connect $IP &>/dev/null
fi
echo "Done!"
If your device is running Android 11 or above then you can accomplish this without initially needing to connect the phone to PC via USB as mentioned in the official Android developer site (link).
If the Android device is running Android 10 or below then there is no straightforward way without initially connecting USB (link).
Duplicate of
Run/install/debug Android applications over Wi-Fi?
Adding to it, if you install Android Studio Bumblebee version and your device OS is 11+,
there is a direct option to connect via Android Studio.
Here are the steps:
First, Connect your phone and machine on the same wifi network
on Phone:
Go to Developer Options -> Wireless Debugging -> Turn on the switch then tap on text Wireless Debugging
Select Pair device with QR code
on Desktop:
1.
Expand this drop down
Select Pair
Scan the QR code wait for some time and that's it!
Note: It disconnects frequently, has to be a bug which we should wait to get fixed

Is there an ADB command to enable/disable mobile data?(Rooted Device)

I'm looking for an adb shell command to enable/disable mobile data. The app is only being tested on rooted devices so I have adb root privileges. I looked around and found that wifi can be enabled/disabled with:
$ adb shell svc wifi enable
$ adb shell svc wifi disable
These work for me but I wasn't able to find anything for mobile data.
Any reference to a list of adb shell svc commands that can be performed would be appreciated as well. I know there's a list of key input commands from this question and obviously I'm aware of the developer page but there's no mention of svc.
Lastly, what is svc?
I found the answer to my question:
To enable mobile data:
$ adb shell svc data enable
To disable mobile data:
$ adb shell svc data disable

Getting Error while configuring ADB

Trying to configure ADB so that I can debug it via wifi, I have checked on Google but not getting proper resolution.
Tell me what to enter in port address, device ip.
Moreover setprop service.adb.tcp.port command is running when i give space between set and prop, bit confuse...
Seeking for help
I got the following article, and wanted help in the same context.
disconnect device from usb then tell it to listen on 4455
adb tcpip 4455
restarting in TCP mode port: 4455
connect to the device using a specified ip:port. my device is using wifi
adb connect 192.168.1.103:4455
connected to 192.168.1.103:4455
now do normal adb commands over tcp
adb shell
when your done, you can put it back in USB mode
adb usb
restarting in USB mode
Help me
These are commands for the shell of the android device, not for the host system.
Type adb shell to get the device's shell and then enter them. If you get back to the windows shell prompt you are in the wrong place.
However those commands may not be effective on a secured device anyway.
setprop is an Android command and is meant to be used in a terminal after su on the device (you can do adb shell setprop ... but when you adb shell stop adbd you'll loose your device connection). The easier way if already connected via USB is adb tcpip 8600 and then adb connect IP_OF_PHONE:8600 -- but this will only work if adb shell will get you a root prompt (starts with # and not $). Be aware that anyone on your wifi network can access your device!

Simulate Wireless Network in Emulator

How do I simulate a WiFi network using the Emulator?
Simple answer: you can't. Also, this has been asked before (and yes, it's still valid).
The emulator does not emulate WiFi specifically. It emulates a generic network connection. You can use telnet commands to configure its characteristics (e.g., speed, latency).
Here is the solution I came up with for simulating total network connection loss on the emulator:
Write the following script and name it "nonetwork.sh"
netcfg eth0 down
sleep 10
netcfg eth0 up
netcfg eth0 dhcp
Upload the following script to the emulator via this command:
adb push nonetwork.sh /data/local/nonetwork.sh
Change the permissions
adb shell chmod 700 /data/local/nonetwork.sh
Run it
adb shell /data/local/nonetwork.sh
While the network is down on the device you'll lose access to adb as well but once the connection is re-established it will work again. Hope this helps.

Categories

Resources