- How to turn off Wifi via ADB?
- 13 Answers 13
- Using «svc» through ADB (rooted required):
- Using Key Events through ADB:
- Via cmd command on Android 11 & 12 (no-root-required)
- Setting up a wireless network in Windows
- Get the right equipment
- Setting up the modem and Internet connection
- Positioning the wireless router
- Securing your wireless network
- Connect a PC to your wireless network
How to turn off Wifi via ADB?
Im automating a testing procedure for wifi calling and I was wondering is there a way to turn off/on wifi via adb? I would either like to disable/enable wifi or kill wifi calling (com.movial.wificall) and restart it. Is it possible to do this all via adb and shell commands? so far I have found:
android.net.wifi.WifiManager setWifiEnabled(true/false)
13 Answers 13
Using «svc» through ADB (rooted required):
adb shell su -c 'svc wifi enable'
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.
Thank you very much for the post. Its not quite the same on my phone, but since you posted the input events I figured out it is key events 19 and 23. Thanks Again. : )
to use those keyevents to toggle wifi in Android 5.0 you would probably have to hit different events then in answer. I would say adb shell input keyevent 19 & adb shell input keyevent 19 & adb shell input keyevent 23 (the first one can be 20 as well) — this is working if there is no option selected when you enter the wifi settings screen (if there is, you should skip the first event)
I was searching for the same to turn bluetooth on/off, and I found this:
adb shell svc wifi enable|disable
I’ve tried using this code on 6 different devices ranging from Gingerbread to JellyBean, rooted to non-rooted and I couldn’t get it to work on any of them. What devices are you able to get this to work on?
@Nefarii — for this I needed to request adb shell into the device. then request su permission. then I was able to run these commands properly.
this works on my Samsung S8 non-rooted, the adb shell su -c ‘svc wifi enable’ didn’t (since it wasn’t rooted)
Simple way to switch wifi on non-rooted devices is to use simple app:
public class MainActivity extends Activity < @Override public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WifiManager wfm = (WifiManager) getSystemService(Context.WIFI_SERVICE); try < wfm.setWifiEnabled(Boolean.parseBoolean(getIntent().getStringExtra("wifi"))); >catch (Exception e) < >System.exit(0); > >
$ adb shell am start -n org.mytools.config/.MainActivity -e wifi true $ adb shell am start -n org.mytools.config/.MainActivity -e wifi false
I tweaked this a little bit and changed the activity for a BroadcastReceiver so it can be added to any app and It won’t show any GUI.
Via cmd command on Android 11 & 12 (no-root-required)
adb shell cmd -w wifi set-wifi-enabled enabled
adb shell cmd -w wifi set-wifi-enabled disabled
the command returns the Invalid args for set-wifi-enabled: java.lang.IllegalArgumentException: Expected ‘enabled’ or ‘disabled’ as next arg but got ‘disable’ / [. ] but got ‘enable’ error if running enable / disable instead of enabled / disabled .
- go to location android/android-sdk/platform-tools
- shift +right click
- open cmd here and type the following commands
- adb shell
- su
- svc wifi enable/disable
adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings adb shell input keyevent 19 && adb shell input keyevent 19 && adb shell input keyevent 23
and only works on window’s prompt, maybe because of some driver
The adb shell svc wifi enable|disable solution only works with root permissions.
This was particularly useful: my Genymotion emulators randomly start up with the wifi available but sometimes not selected. I used this: shell ‘am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings; input keyevent 20; input keyevent 23’
It will work if you use with quotes:
adb shell "svc wifi enable"
if you try this in a command prompt it will work.. It will not open anything.. the action will be performed in background
settings put global wifi_on 0 settings put global wifi_scan_always_enabled 0
Sometimes, if done during boot (i.e. to fix bootloop such as this), it doesn’t apply well and you can proceed also enabling airplane mode first:
settings put global airplane_mode_on 1 settings put global wifi_on 0 settings put global wifi_scan_always_enabled 0
Other option is to force this with:
while true; do settings put global wifi_on 0; done
Tested in Android 7 on LG G5 (SE) with (unrooted) stock mod.
All these input keyevent combinations are SO android/hardware dependent, it’s a pain.
However, I finally found the combination for my old android 4.1 device :
adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings adb shell "input keyevent KEYCODE_DPAD_LEFT;input keyevent KEYCODE_DPAD_RIGHT;input keyevent KEYCODE_DPAD_CENTER"
The following method doesn’t require root and should work anywhere (according to docs, even on Android Q+, if you keep targetSdkVersion = 28 ).
- Make a blank app.
- Create a ContentProvider :
class ApiProvider : ContentProvider() < private val wifiManager: WifiManager? by lazy(LazyThreadSafetyMode.NONE) < requireContext().getSystemService(WIFI_SERVICE) as WifiManager? >private fun requireContext() = checkNotNull(context) private val matcher = UriMatcher(UriMatcher.NO_MATCH).apply < addURI("wifi", "enable", 0) addURI("wifi", "disable", 1) >override fun query(uri: Uri, projection: Array?, selection: String?, selectionArgs: Array?, sortOrder: String?): Cursor? < when (matcher.match(uri)) < 0 -> < enforceAdb() withClearCallingIdentity < wifiManager?.isWifiEnabled = true >> 1 -> < enforceAdb() withClearCallingIdentity < wifiManager?.isWifiEnabled = false >> > return null > private fun enforceAdb() < val callingUid = Binder.getCallingUid() if (callingUid != 2000 && callingUid != 0) < throw SecurityException("Only shell or root allowed.") >> private inline fun withClearCallingIdentity(block: () -> T): T < val token = Binder.clearCallingIdentity() try < return block() >finally < Binder.restoreCallingIdentity(token) >> override fun onCreate(): Boolean = true override fun insert(uri: Uri, values: ContentValues?): Uri? = null override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array?): Int = 0 override fun delete(uri: Uri, selection: String?, selectionArgs: Array?): Int = 0 override fun getType(uri: Uri): String? = null >
adb shell content query --uri content://wifi/enable adb shell content query --uri content://wifi/disable
Depending on your device you may need additional permissions.
Setting up a wireless network in Windows
A wireless network at home lets you get online from more places in your house. This article describes the basic steps for setting up a wireless network and starting to use it.
Get the right equipment
Before you can set up your wireless network, here’s what you’ll need:
Broadband Internet connection and modem. A broadband Internet connection is a high-speed Internet connection. Digital Subscriber Line (DSL) and cable are two of the most common broadband connections. You can get a broadband connection by contacting an Internet service provider (ISP). Typically, ISPs that provide DSL are telephone companies and ISPs that provide cable are cable TV companies. ISPs frequently offer broadband modems. Some ISPs also offer combination modem/wireless routers. You can also find these at computer or electronics stores, and online.
Wireless router. A router sends info between your network and the Internet. With a wireless router, you can connect PCs to your network using radio signals instead of wires. There are several different kinds of wireless network technologies, which include 802.11a, 802.11b, 802.11g, 802.11n, 802.11ac, and 802.11ax.
Wireless network adapter. A wireless network adapter is a device that connects your PC to a wireless network. To connect your portable or desktop PC to your wireless network, the PC must have a wireless network adapter. Most laptops and tablets—and some desktop PCs—come with a wireless network adapter already installed.
To check whether your PC has a wireless network adapter:
- Select Start , type device manager in the search box, and then select Device Manager.
- Expand Network adapters.
- Look for a network adapter that might have wireless in the name.
Setting up the modem and Internet connection
After you have all the equipment, you’ll need to set up your modem and Internet connection. If your modem wasn’t set up for you by your Internet service provider (ISP), follow the instructions that came with your modem to connect it to your PC and the Internet. If you’re using Digital Subscriber Line (DSL), connect your modem to a phone jack. If you’re using cable, connect your modem to a cable jack.
Positioning the wireless router
Put your wireless router somewhere where it will receive the strongest signal with the least amount of interference. For better results, follow these tips:
Place your wireless router in a central location. Place the router as close to the center of your home as possible to increase the strength of the wireless signal throughout your home.
Position the wireless router off the floor and away from walls and metal objects, such as metal file cabinets. The fewer physical obstructions between your PC and the router’s signal, the more likely that you’ll be using the router’s full signal strength.
Reduce interference. Some networking equipment uses a 2.4 gigahertz (GHz) radio frequency. This is the same frequency as most microwaves and many cordless phones. If you turn on the microwave or get a call on a cordless phone, your wireless signal might be temporarily interrupted. You can avoid most of these issues by using a cordless phone with a higher frequency, such as 5.8 GHz.
Securing your wireless network
Security is always important; with a wireless network, it’s even more important because your network’s signal could be broadcast outside your home. If you don’t help secure your network, people with PCs nearby could access info stored on your network PCs and use your Internet connection.
To help make your network more secure:
Change the default user name and password. This helps protect your router. Most router manufacturers have a default user name and password on the router and a default network name (also known as the SSID). Someone could use this info to access your router without you knowing it. To help avoid that, change the default user name and password for your router. See the documentation for your device for instructions.
Set up a security key (password) for your network. Wireless networks have a network security key to help protect them from unauthorized access. We recommend using Wi-Fi Protected Access 3 (WPA3) security if your router and PC support it. See the documentation for your router for more detailed info, including what type of security is supported and how to set it up.
Some routers support Wi-Fi Protected Setup (WPS). If your router supports WPS and it’s connected to the network, follow these steps to set up a network security key:
- Do one of the following, depending on which version of Windows is running on your PC:
- In Windows 7 or Windows 8.1, select Start, start typing Network and Sharing Center, and then choose it in the list.
- In Windows 10, select Start , then select Settings >Network & Internet >Status >Network and Sharing Center.
- In Windows 11, select Start, type control panel, then select Control Panel >Network and Internet >Network and Sharing Center.
- Select Set up a new connection or network.
- Select Set up a new network, then choose Next.
The wizard will walk you through creating a network name and a security key. If your router supports it, the wizard will default to Wi‑Fi Protected Access (WPA2 or WPA3) security. We recommend that you use WPA3 if you can, because it offers better security than WPA2, WPA, or Wired Equivalent Privacy (WEP) security. With WPA3, WPA2 or WPA you can also use a passphrase, so you don’t have to remember a cryptic sequence of letters and numbers.
Write down your security key and keep it in a safe place. You can also save your security key on a USB flash drive by following the instructions in the wizard. (Saving your security key to a USB flash drive is available in Windows 8 and Windows 7, but not in Windows 10 or Windows 11.)
Use a firewall. A firewall is hardware or software that can help protect your PC from unauthorized users or malicious software (malware). Running a firewall on each PC on your network can help control the spread of malicious software on your network, and help protect your PCs when you’re accessing the Internet. Windows Firewall is included with this version of Windows.
Connect a PC to your wireless network
- Select the Network or Wifi icon in the notification area.
- In the list of networks, choose the network that you want to connect to, and then select Connect.
- Type the security key (often called the password).
- Follow additional instructions if there are any.
If you have problems with your Wi-Fi network when using Windows 10, see Fix Wi-Fi problems in Windows for advanced troubleshooting info.
- Select the Network icon in the notification area, then select the > icon next to the Wi-Fi quick setting to see a list of available networks.
- Choose the network that you want to connect to, and then select Connect.
- Type the security key (often called the password).
- Follow additional instructions if there are any.
If you have problems with your Wi-Fi network when using Windows 11, see Fix Wi-Fi problems in Windows for advanced troubleshooting info.