How to Turn Off Wi-Fi via ADB on Android Devices

How to Turn Off Wi-Fi via ADB on Android Devices

30 November 2024 Stephan Petzl Leave a comment Tech-Help

Automating tasks on Android devices can greatly enhance testing procedures, especially for features like Wi-Fi calling. This guide provides a comprehensive overview of how to enable or disable Wi-Fi on Android devices using ADB (Android Debug Bridge) commands. This method can be particularly useful for developers and testers looking to automate their processes.

Using ADB Commands

ADB provides a shell interface for interacting with your device. To manipulate Wi-Fi settings, you can use the following commands:

For Rooted Devices

  • Enable Wi-Fi:
    adb shell su -c 'svc wifi enable'
  • Disable Wi-Fi:
    adb shell su -c 'svc wifi disable'

These commands require root access, which allows you to directly manipulate system settings.

For Non-Rooted Devices

If your device is not rooted, you can utilize key events to navigate the settings menu and toggle Wi-Fi:

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

This approach simulates user interaction by opening the Wi-Fi settings page and toggling the Wi-Fi state. Note that key events may vary based on device and Android version.

Using CMD Command on Android 11 & 12

For newer Android versions, a no-root-required method is available:

  • Enable Wi-Fi:
    adb shell cmd -w wifi set-wifi-enabled enabled
  • Disable Wi-Fi:
    adb shell cmd -w wifi set-wifi-enabled disabled

Advanced Techniques and Considerations

For a more advanced setup, you can create an Android application with a ContentProvider to toggle Wi-Fi. This method is complex but offers fine-grained control over Wi-Fi state without requiring root access. Details on creating such an application are available in our advanced testing techniques documentation.

Seamless Test Automation with Repeato

For those looking to streamline their testing process, Repeato offers a no-code test automation tool for iOS and Android. It allows you to create, run, and maintain automated tests using computer vision and AI. Utilizing Repeato, you can execute ADB commands efficiently via script steps, ensuring proper timing and sequence in your automated testing workflows. This integration makes managing Wi-Fi states during testing straightforward and efficient.

Explore more about how Repeato can enhance your testing processes and ensure robust app performance by visiting our documentation section.

Like this article? there’s more where that came from!