10

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.

Community
  • 1
  • 1
Ganesh Subramanian
  • 193
  • 1
  • 2
  • 9

4 Answers4

8

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.

chgsilva
  • 768
  • 7
  • 20
  • I'm trying programmatically but not working. Here is the snippet `val command = "adb shell wpa_cli remove_network ${wifiManager.connectionInfo.networkId}" val process = Runtime.getRuntime().exec(arrayOf("su", command))` – Muhammad Babar May 28 '20 at 13:07
4

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
Always Learning
  • 2,623
  • 3
  • 20
  • 39
0

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

Rajeev
  • 119
  • 4
-3

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

Community
  • 1
  • 1
Stanley Ko
  • 3,383
  • 3
  • 34
  • 60