Daniels Blog
28Nov/162

Shell commands for hardware management in Android 6 Marshmallow / Cyanogenmod 13

android-shellIf you are using an automation app like Tasker, Llama or Automate, you can toggle / turn on / turn off WIFI, Data, GPS etc. with shell commands. I collected these little sniplets for my own setup. They work on my device (Nexus 5x with Cyanogenmod 13). They should work on other Android 6 devices as well.

Run all shell commands as root.

WIFI

svc wifi enable
svc wifi disable

DATA

svc data enable
svc data disable

GPS

# turn GPS on
settings put secure location_providers_allowed +gps 
# turn GPS off
settings put secure location_providers_allowed -gps 

Flightmode

# Flightmode on
settings put global airplane_mode_on 1
am broadcast -a android.intent.action.AIRPLANE_MODE

# Flightmode off
settings put global airplane_mode_on 0
am broadcast -a android.intent.action.AIRPLANE_MODE

Turn off screen without locking device (emulate power button keypress)

input keyevent 26

Toggle Network mode (2G/3G/4G)
I have been searching for a long time, but there seems to be no direct way to do this.
The best solution I have found requires xposed framework and gravitybox installed.

I found the available network types here:
http://android.stackexchange.com/questions/44347/simple-way-to-toggle-between-2g-and-3g-connection

Identify the right settings for your device:

# Disable your automation app, set networking mode with the preferences in your devices GUI.
# Then run

settings get global preferred_network_mode 

# The currently set network mode number will be shown in shell

Just send the following intent to toggle network mode:

Send Intent [
Action: gravitybox.intent.action.CHANGE_NETWORK_TYPE
Cat: None
Mime Type:
Data:
Extra: networkType:1
Extra:
Package:
Class:
Target: Broadcast Receiver]

networkType enum values are: 
0: WCDMA Preferred 
1: GSM only <-- This would be "2G" on GSM networks
2: WCDMA only <--WCDMA is "3G" on GSM networks. You may know it as HSPA
3: GSM auto (PRL)
4: CDMA auto (PRL)
5: CDMA only <-- This would be "2G" on CDMA networks
6: EvDo only <-- EvDo is "3G" on CDMA networks
7: GSM/CDMA auto (PRL)
8: LTE/CDMA auto (PRL)
9: LTE/GSM auto (PRL)
10: LTE/GSM/CDMA auto (PRL)
11: LTE only
12: "unknown"

Disable captive portal detection
This needs to be set at boottime. It doesn't survive a reboot.

settings put global captive_portal_detection_enabled 0
settings put global captive_portal_server 127.0.0.1

Force a DNS of your choice
Google removed the option to change the DNS for mobile data. (ugly, ugly, ugly)
You can force your device to use your fav DNS with iptables for mobile data:

/system/bin/iptables -t nat -A OUTPUT ! -o wlan0 -p udp --dport 53 -j DNAT --to-destination 213.73.91.35:53

In case you have found a better way to switch network mode, I'd really appreciate your comment 🙂