简体   繁体   中英

How to enable airplane mode on android using adb

I have an android phone on which I want to programatically toggle airplane mode.

I've been unable to root this device, which has prevented any apps I'm running from having permission to edit these settings, so am using adb to open the settings menu and toggle the switch there.

Executing the following adb shell commands will work in some cases:

//Get airplane mode settings menu
adb -s ${device} shell am start -a android.settings.AIRPLANE_MODE_SETTINGS

//Hit enter (as many times as required)
adb -s ${device} shell input keyevent KEYCODE_ENTER

//Check the state of airplane mode to determine when successful
adb -s ${device} shell settings get global airplane_mode_on

However, this only work if the menu containing the airplane mode switch is the first menu option listed on the settings page, since "android.settings.AIRPLANE_MODE_SETTINGS" will only navigate to the menu where the setting is found. It will not perform any focussing on the setting itself, so the following command to hit enter will press the wrong setting.

If using this approach, is there a way to identify/focus only the airplane toggle switch, or otherwise check which menu item is in focus so the items can be iterated until the correct one is found?

Additionally, it would be great if there was a way to execute these adb commands without requiring the phone to be plugged into a PC - ie can the adb client be run on the phone itself (and then trick itself into thinking it is a connected device).

And finally - the end goal here is simply to toggle airplane mode programatically on demand (ultimately the request will be triggered by a node.js process running in termux app). If there is a better way to achieve this without using adb that doesn't require root permissions, this would also work.

Here are the ADB commands to turn on and turn of airplane mode:

Turn on:

adb shell settings put global airplane_mode_on 1
adb shell am broadcast -a android.intent.action.AIRPLANE_MODE

Turn off:

adb shell settings put global airplane_mode_on 0
adb shell am broadcast -a android.intent.action.AIRPLANE_MODE

I suspect this is an incredibly niche requirement, but if anyone is interested in how to toggle airplane mode on a non-rooted android phone using adb commands, where the UI airplane mode toggle button is not the first item in the settings activity.... read below!

As previously mentioned, load the screen containing the airplane mode button:

adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS

Now to check the current state of the UI:

adb shell uiautomator dump
adb pull /sdcard/window_dump.xml

STDOUT of the first command above can be checked to determine exactly where the uiautomator dump is sent by default.

To navigate the settings menu, use the DPAD UP/DOWN keyevents:

adb shell input keyevent KEYCODE_DPAD_DOWN
adb shell input keyevent KEYCODE_DPAD_UP

Depending on the device, I've found that using the above options either results in nodes being selected or focused. The output of the UI dump can be parsed, and in the DOM search for either "node[selected=true]" or "node[focused=true]" depending on which the device uses. I'm eventually checking to see when node.attr('text').toUpperCase.contains('PLANE MODE') to check when the airplane mode setting has been reached, and then finally executing:

adb shell input keyevent KEYCODE_ENTER

to activate the switch.

For now this covers the devices I've tried it on, but performance is not great if there is a lot of scrolling in the menu to do. I'd be interested to know if there's a way to hold uiautomator dump output in memory on the device and parse the XML there to avoid unnecessary I/O of outputting to a file, pulling to a local drive and reading it back into memory there.

if [[ "$(adb shell dumpsys wifi | grep mAirplaneModeOn)" == "mAirplaneModeOn false" ]]; then            

    adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS
        && input keyevent 19
        && input keyevent 23
        && input keyevent 4
fi 

Create bat file:

adb -s 3e31d8d97d95 shell am start -a android.settings.AIRPLANE_MODE_SETTINGS; while [ 0 -le 5 ]; do input keyevent 23; sleep 0.1;input keyevent 23;sleep 13; echo 'done'; done;

Make sure the "stay awake" is ON in debug menu. Also make sure this is more "gray" - focues: 在此处输入图像描述

Run the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM