It might be necessary to control Android device connectivity state. For example, to turn the network off while running integration tests. Often there is no way to do that programmatically. Fortunately, Android Debug Bridge has some useful shell commands.
svc command allows to control the following types of connections:
- Mobile data
Should be run inside the interactive shell. Execute
adb shell first, or prefix each command with
svc wifi enable
svc wifi disable
Enable mobile data
svc data enable
Disable mobile data
svc data disable
svc bluetooth enable
svc bluetooth disable
svc nfc enable
svc nfc disable
Set the ‘keep awake while plugged in’ setting.
svc power stayon [true|false|usb|ac|wireless]
Perform a runtime shutdown and reboot device with specified reason.
svc power reboot [reason]
Perform a runtime shutdown and power off the device.
svc power shutdown
Set the current usb function. If function is blank, sets to charging.
svc usb setFunctions [function]
Sets the functions which, if the device was charging, become current on screen unlock. If function is blank, turn off this feature.
svc usb setScreenUnlockedFunctions [function]
Gets the list of currently enabled functions
svc usb getFunctions
Possible values of [function] are any of ‘mtp’, ‘ptp’, ‘rndis’, ‘midi’