Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable / disable wifi via bash scripting on Android

I am trying to enable / disable the wifi device in my android device in a bash script. I am using the terminal emulator and the program Script Manager to execute bash scripts on the phone (which is a rooted Nexus One). The normal way to do this in linux would be something like

ifconfig eth0 up 

That gives me

"eth0: no such device"

Trying iwconfig eth0 gives the same, and

iwconfig 

gives a list of devices with no eth0 on it. Now if I turn on wifi (manually from the GUI) and type

iwconfig

it shows eth0 with a lot of info.

My question is: How can I use the command line to turn on / off wifi? And why is eth0 disappearing from the device list when wifi is disabled?

thanks.

like image 489
AHaahr Avatar asked Jun 23 '11 23:06

AHaahr


People also ask

What is the method used to enable the WIFI in Android Studio?

setWifiEnabled(false); So you can enable or disable WiFi using WifiManger class. You need to add extra permissions in AndroidManifest. xml to enable or disable Wifi.

How do I connect to a specific Wi Fi network in android programmatically?

First and foremost, add permissions in your manifest file. These 4 permissions are required to make changes in your device network connectivity. Setup configuration to connect one specific wifi using WifiConfiguration. WifiConfiguration config = new WifiConfiguration();


1 Answers

While it doesn't work in my non-rooted phone for obvious reasons, the results I'm obtaining are completely different while wifi is connected:

$ ifconfig eth0
eth0: ip 192.168.0.102 mask 255.255.255.0 flags [up broadcast running multicast]
$ ifconfig eth0 up
error: SIOCSIFFLAGS (Permission denied)

If you just want to turn wifi on or off you should use:

# svc wifi enable

or

# svc wifi disable

to see the help message

# svc wifi
Control the Wi-Fi manager

usage: svc wifi [enable|disable]
         Turn Wi-Fi on or off.

       svc wifi prefer
          Set Wi-Fi as the preferred data network
like image 129
Diego Torres Milano Avatar answered Oct 07 '22 16:10

Diego Torres Milano