Connecting to Wifi Using Adb Shell

Connecting to WiFi using adb shell

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

Connect to password protected wifi network using adb shell

This is possible to achieve using wpa_cli, command line interface of wpa_supplicant:

# Get to the shell
adb root
adb shell

# Get to wpa_cli prompt
wpa_cli -p /data/misc/wifi/sockets/ -i wlan0

# Add new WiFi network
add_network
set_network 0 auth_alg OPEN
set_network 0 key_mgmt WPA-PSK
set_network 0 ssid "network_name"
set_network 0 proto RSN
set_network 0 mode 0
set_network 0 psk "password"

# Connect to it
select_network 0
enable_network 0
reassociate

# Check the status
status

In the above list of commands, add_network command will output the index of the new network, which should be used for the subsequent commands. In this example, this index is 0.

android adb turn on wifi via adb

Unfortunately the only way I could resolve my problem is to root the device.

Here is a good tutorial for Nexus S:

http://nexusshacks.com/nexus-s-root/how-to-root-nexus-s-or-nexus-s-4g-on-ics-or-gingerbread/

How to turn off Wifi via ADB?

Using "svc" through ADB (rooted required):

Enable:

adb shell su -c 'svc wifi enable'

Disable:

adb shell su -c 'svc wifi disable'

Using Key Events through ADB:

adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings
adb shell input keyevent 20 & adb shell input keyevent 23

The first line launch "wifi.WifiSettings" activity which open the WiFi Settings page. The second line simulate key presses.

I tested those two lines on a Droid X. But Key Events above probably need to edit in other devices because of different Settings layout.

More info about "keyevents" here.

How can I use adb over WiFi?

  • Connect Android phone and host machine to same WiFi network
  • Connect Android phone to host machine using USB cable (to start with)
  • Run adb tcpip 5555 from a command prompt
  • Run adb shell "ip addr show wlan0 | grep -e wlan0$ | cut -d\" \" -f 6 | cut -d/ -f 1" to obtain the phone's IP address
  • Disconnect USB cable and run adb connect <ip_address>:5555

You can now view logcat output by running adb logcat or by viewing the Android Monitor tab within Android Studio.



Related Topics



Leave a reply



Submit