I have all the details to connect to a particular access point. I have to use that access point only, so all I require is the command to do it.
-
What do you want to achieve ? – Diego Torres Milano Jan 03 '12 at 07:03
-
1I have to automate a few things which only work with wi-fi.I can use monkey and click on settings->etc., but last thing would be how do i choose the network ABC as there may be more than one networks available. so if i could get a command, i can do that using SSID and password right away :) – Rohan Jan 03 '12 at 07:05
-
To complement the answer: http://stackoverflow.com/questions/10282484/android-adb-turn-on-wifi-via-adb – EthraZa Mar 15 '13 at 20:12
-
I updated the wpa_supplicant.conf file with new SSID and password, enabled the wifi but this doesn't seem to work on Nexus 4, Lolipop build. Wifi turns on but goes back to original SSID, that I was using via UI. I also noticed that my updated file was over written with the earlier SSID. I have change chown to system.wifi . Am I doing something incorrect here? I saw some post talking about sqlite3. Do we need to change anything in db? – TusharG Oct 08 '15 at 11:38
-
wpa_cli way: https://stackoverflow.com/questions/22825443/connect-to-password-protected-wifi-network-using-adb-shell – domen Nov 08 '17 at 09:49
7 Answers
Late to the party, but I came up with a way to accomplish this on a device without root.
It may not be pretty, but it works.
Basically what I propose is to create an app that joins an access point based on EXTRAS
given when starting the app.
The EXTRAS
are then provided using the am
command's -e <KEY> <VALUE>
parameter.
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]
There's more info in the README on Github.
Hope it helps someone.

- 7,941
- 3
- 37
- 50
-
1
-
-
@Suban Dhyako It should tell you in your router settings what type of password/security it's using. Also, in the android wifi screen, if you tap on a network it will tell you the security type. Most routers these days will be set up as WPA2 or WPA, and it seems that typing WPA in the adb command will work for either WPA or WPA2. Also, as a note to the author, this worked wonderfully, thank you. But the multi-line command formatting on the github didn't work for me, while the single line command here did. – gamingexpert13 Oct 27 '21 at 22:42
-
@gamingexpert13 thanks, glad to hear it worked for you. The multiline is probably only working on Linux :) – jpihl Oct 28 '21 at 04:55
-
hey man I am trying to use your app but when I run the command it says "This application is meant to be used with ADB" what am I doing wrong? – dam1ne Nov 16 '22 at 00:17
-
Hi @dam1ne, as far as I remember, the message is shown when you start the application on your phone from the launcher. This app is meant to be started from ADB. Did you follow the instructions in the readme: https://github.com/steinwurf/adb-join-wifi/blob/master/README.rst#usage – jpihl Nov 16 '22 at 09:07
You can add a network entry into the wpa_supplicant.conf yourself (or within your script) Essentially connect manually once, then do:
adb pull /data/misc/wifi/wpa_supplicant.conf
and integrate the network entry into your script for automation. Example simple script:
#!/bin/bash
#
# Get this information by connecting manually once, and do
# adb pull /data/misc/wifi/wpa_supplicant.conf
ADB_PULL="adb pull /data/misc/wifi/wpa_supplicant.conf"
WIRELESS_CTRL_INTERFACE=wlan0
WIRELESS_SSID=Gondolin
WIRELESS_KEY_MGMT="WPA-EAP IEEE8021X"
WIRELESS_EAP=PEAP
WIRELESS_USER=Turgon
WIRELESS_PASSWORD=IdrilCelebrindal
adb start-server
adb wait-for-device
echo "adb connection....[CONNECTED]"
adb root
adb wait-for-device
adb remount
adb wait-for-device
pushd /tmp
rm wpa_supplicant.conf 2>/dev/null # Remove any old one
adbpull_status=`$ADB_PULL 2>&1`
echo -e "\nAttempting: $ADB_PULL"
if [ `echo $adbpull_status | grep -wc "does not exist"` -gt 0 ]; then
echo " wpa_supplicant.conf does not exist yet on your device yet."
echo "This means you have not used your wireless yet."
echo ""
echo "Taking our best shot at creating this file with default config.."
echo "ctrl_interface=$WIRELESS_CTRL_INTERFACE" >> wpa_supplicant.conf
echo "update_config=1" >> wpa_supplicant.conf
echo "device_type=0-00000000-0" >> wpa_supplicant.conf
else
echo $adbpull_status
echo " wpa_supplicant.conf exists!"
fi
echo ""
echo "Add network entry for wpa_supplicant.conf.."
echo "" >> wpa_supplicant.conf
echo "network={" >> wpa_supplicant.conf
echo " ssid=\"$WIRELESS_SSID\"" >> wpa_supplicant.conf
echo " key_mgmt=$WIRELESS_KEY_MGMT" >> wpa_supplicant.conf
echo " eap=$WIRELESS_EAP" >> wpa_supplicant.conf
echo " identity=\"$WIRELESS_USER\"" >> wpa_supplicant.conf
echo " password=\"$WIRELESS_PASSWORD\"" >> wpa_supplicant.conf
echo " priority=1" >> wpa_supplicant.conf
echo "}" >> wpa_supplicant.conf
echo "Pushing wpa_supplicant.conf.."
adb push wpa_supplicant.conf /data/misc/wifi/wpa_supplicant.conf
popd #/tmp
adb shell chown system.wifi /data/misc/wifi/wpa_supplicant.conf
adb shell chmod 660 /data/misc/wifi/wpa_supplicant.conf
echo ""
echo "Finished!"
adb shell am start -a android.intent.action.MAIN -n com.android.settings/.Settings
echo "Please toggle wifi off/on now.. (ifconfig not sufficient, monkey this)"

- 1,201
- 11
- 9
-
3One could just use: `wpa_passphrase ssid passphrase` on an Ubuntu box and copy the output to the android device. It has worked for me on a samsung tablet. – wojciii Aug 11 '12 at 11:22
-
5Yes for rooted devices. But it really helped me to unlock my devices. Thank you very-much for useful paths. Instead of toggling through settings I used `svc wifi disable` and then `svc wifi enable`. – ony Aug 12 '14 at 13:40
-
after 4 hours of intensive searching for solution how to connect to wifi, where absolutely nothing worked, this solution was the magic-spell! Also added edit suggestion for svc restart, like @ony suggests – Deko Jan 07 '17 at 10:05
As an add-on: you can enable wifi via svc as root on the device
svc wifi enable
and disable via
svc wifi disable

- 35,772
- 9
- 166
- 188
-
This requires the device to be rooted and we should not suggest users to root the device because of security reasons. – Humble_PrOgRaMeR Aug 02 '17 at 08:09
-
@RajA.P.: Thank you for the feedback. Seemingly all methods except [@jpihl](https://stackoverflow.com/a/37303412/1587329)'s require the device to be rooted, they just did not say this explicitly. If the downvote was from you: did you downvote them all? – serv-inc Aug 02 '17 at 09:47
-
I had tried the above commands which said me the device to be rooted and I am using my enterprise device, which i dont want to compromise. – Humble_PrOgRaMeR Aug 02 '17 at 10:30
-
@RajA.P.: in that case, the above answer by jpihl might just work (as you see, this answer is a bit older) – serv-inc Aug 02 '17 at 21:07
-
Whether you can use this or not depends on the device. I can use this on my S21 FE, but I'm not allowed `cmd wifi connect-network`. – DariusL Mar 02 '23 at 13:25
You can use the command adb shell cmd -w wifi connect-network with these parameters
- connect-network open|owe|wpa2|wpa3 [] [-m] [-d] [-b ] [-r auto|none|persistent|non_persistent] Connect to a network with provided params and add to saved networks list open|owe|wpa2|wpa3 - Security type of the network. - SSID 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) -m - Mark the network metered. - 'wpa3' - WPA-3 PSK networks -d - Mark the network autojoin disabled. -h - Mark the network hidden. -p - Mark the network private (not shared). -b - Set specific BSSID. -r auto|none|persistent|non_persistent - MAC randomization scheme for the network
To connect to a wifi network 'Home' with a wpa2 authentication and passphrase as 'qwertyuiop' use
adb shell cmd -w wifi connect-network Home wpa2 qwertyuiop
To connect to an open wifi network 'Public' use
adb shell cmd -w wifi connect-network Public open

- 311
- 2
- 8
-
-
-
my wifi SSID/name has spaces and a ! in it. 'Fr!tzBox 3423' How do I pass that? It gives Invalid args for connect-network: java.lang.IllegalArgumentException: Unknown network type 3423 – ir2pid Mar 03 '23 at 10:09
-
the solution to escape space in SSID is to use \. The command would become `adb shell cmd wifi connect-network 'FRITZ!Box\ 3423' wpa2 password123` – ir2pid Mar 03 '23 at 16:54
As an another add-on: although my device was rooted I got
remote object ''/data/misc/wifi/wpa_supplicant.conf'' does not exist
error while trying to execute adb pull
. It happens because adb
itself doesn't run in ROOT mode. To work this around you can execute something like this
adb shell "su -c 'cp -R /data/misc/wifi/wpa_supplicant.conf /data/misc/wpa_supplicant.conf'"
adb shell "su -c 'chmod -R 777 /data/misc/wpa_supplicant.conf'"
adb pull /data/misc/wpa_supplicant.conf
adb shell "su -c 'rm /data/misc/wpa_supplicant.conf'"

- 21
- 2
I solve the problem by this:
adb pull /data/misc/wifi/wpa_supplicant.conf ~/Desktop
,and then edit the file, add network module, my whole conf file is:
##### wpa_supplicant configuration file template #####
update_config=1
ctrl_interface=DIR=/data/system/wpa_supplicant GROUP=wifi
eapol_version=1
ap_scan=1
fast_reauth=1
network={
ssid="your ssid"
psk="your pswd"
key_mgmt=WPA-PSK
priority=241
}
Then rm the origin file, add push it to /data/misc/wifi
folder, reboot your device.Please note, different device has different content above the network line,don't modify that part.

- 893
- 11
- 13
super late but i hope this'll help anybody who may stumble upon this thread.
if you're trying the adb pull method but received "remote object does not exist", try this:
in the same command prompt box,
- type
adb root
to restart adb as root. click enter. - Now type
adb shell
, click enter. makes sure the prompt showsroot@[device]:
- At the # prompt type
cd /data/misc/wifi
click enter. - Lastly type
cat wpa_supplicant.conf
click enter.
this should dump data of WiFi you've previously connected to on your phone, to your pc screen.
these commands worked on my unrooted device after running into the “remote object does not exist” issue.
-
C:\Program Files (x86)\Minimal ADB and Fastboot>adb shell........................................................ shell@android:/ $ cd /data/misc/wifi................................................................................................ /system/bin/sh: cd: /data/misc/wifi: Permission denied................................................................... 2|shell@android:/ $ cat wpa_supplicant.conf............................................................................... – user2340356 Aug 19 '17 at 19:50
-
/system/bin/sh: cat: wpa_supplicant.conf: No such file or directory................................................. 1|shell@android:/ $ cd /data/misc/wifi............................................................................................. /system/bin/sh: cd: /data/misc/wifi: Permission denied............................................................... – user2340356 Aug 19 '17 at 19:52
-
that is what i get when i run those commands :( . i have a samsung galaxy camera. android version 4.1.2. Maybe there is a security bug and i can use it and retrieve the wifi passwords? – user2340356 Aug 19 '17 at 19:52
-