How to turn off Wifi via ADB?

前端 未结 13 2159
北荒
北荒 2020-11-29 17:37

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

相关标签:
13条回答
  • 2020-11-29 17:54

    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);
        }
    }
    

    AndroidManifest.xml:

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    

    ADB commands:

    $ adb shell am start -n org.mytools.config/.MainActivity -e wifi true
    $ adb shell am start -n org.mytools.config/.MainActivity -e wifi false
    
    0 讨论(0)
  • 2020-11-29 17:54

    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"
    
    0 讨论(0)
  • 2020-11-29 17:54

    ADB Connect to wifi with credentials :

    You can use the following ADB command to connect to wifi and enter password as well :

    adb wait-for-device shell am start -n com.android.settingstest/.wifi.WifiSettings -e WIFI 1 -e AccessPointName "enter_user_name" -e Password "enter_password"

    0 讨论(0)
  • 2020-11-29 18:02

    I was searching for the same to turn bluetooth on/off, and I found this:

    adb shell svc wifi enable|disable
    
    0 讨论(0)
  • 2020-11-29 18:03

    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.

    0 讨论(0)
  • 2020-11-29 18:04

    In Android tests I'm doing so: InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("svc wifi enable") InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("svc wifi disable")

    0 讨论(0)
提交回复
热议问题