Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we programatically enable/disable USB debugging on Android devices? [duplicate]

Tags:

android

Is there any way to turn USB debugging on/off programmatically on Android devices?

like image 370
User10001 Avatar asked Sep 13 '13 09:09

User10001


People also ask

Is disabling USB debugging safe?

Background: Trustwave recommends that mobile devices should not be set to USB Debugging mode. When a device is in USB Debugging mode, a computer connected to the device can read all data, run commands, and install or remove apps. The security of the device settings and data could be compromised.

What happens when you enable USB debugging?

Before you can use the debugger and other tools, you need to enable USB debugging, which allows Android Studio and other SDK tools to recognize your device when connected via USB. To enable USB debugging, toggle the USB debugging option in the Developer Options menu.

Can USB debugging damage your phone?

It will only install your app and lets you test and debug the app. No performance degradation is done. However the application will still occupy the memory on your phone. So it will definitely consume your internal memory like every other application does.

Should I allow USB debugging on my phone?

Unless you regularly use ADB and connect your Android device to your PC, you shouldn't leave USB Debugging enabled all the time. It's fine to leave on for a few days while you're working on something, but there's no need to have it enabled when you're not regularly using it.


2 Answers

Hi this is my first post on here, and normally I wouldn't bother but I see no one wanted to give you the answer despite there being multiple ways to do so. This is all from my app, I'm "idone" on xda-dev btw. Also some of this code maybe Samsung MSMxxxx specific

If you have root you can indeed. And here are 3 ways to do so despite other people saying otherwise Method 1(broadcast secret code) Method 2(set sys.usb.config) Method 3(set settings global adb_enabled 1)

public  String[] SET_DM_PORT_STATUS_LIST = new String[9];{
    SET_DM_PORT_STATUS_LIST[0] = "setMTP";
    SET_DM_PORT_STATUS_LIST[1] = "setMTPADB";
    SET_DM_PORT_STATUS_LIST[2] = "setPTP";
    SET_DM_PORT_STATUS_LIST[3] = "setPTPADB";
    SET_DM_PORT_STATUS_LIST[4] = "setRNDISDMMODEM";
    SET_DM_PORT_STATUS_LIST[5] = "setRMNETDMMODEM";
    SET_DM_PORT_STATUS_LIST[6] = "setDMMODEMADB";
    SET_DM_PORT_STATUS_LIST[7] = "setMASSSTORAGE";
    SET_DM_PORT_STATUS_LIST[8] = "setMASSSTORAGEADB";}

public  String[] SET_DM_PORT_CONFIG_LIST = new String[9];{
    SET_DM_PORT_CONFIG_LIST[0] = "mtp";
    SET_DM_PORT_CONFIG_LIST[1] = "mtp,adb";
    SET_DM_PORT_CONFIG_LIST[2] = "ptp";
    SET_DM_PORT_CONFIG_LIST[3] = "ptp,adb";
    SET_DM_PORT_CONFIG_LIST[4] = "rndis,acm,diag";
    SET_DM_PORT_CONFIG_LIST[5] = "rmnet,acm,diag";
    SET_DM_PORT_CONFIG_LIST[6] = "diag,acm,adb";
    SET_DM_PORT_CONFIG_LIST[7] = "mass_storage";
    SET_DM_PORT_CONFIG_LIST[8] = "mass_storage,adb";}

Process su = Runtime.getRuntime().exec("su");
             DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("am broadcast -a android.provider.Telephony.SECRET_CODE -d android_secret_code://" + SET_DM_PORT_STATUS_LIST[paramInt]+"\n");
outputStream.writeBytes("setprop sys.usb.config " + SET_DM_PORT_CONFIG_LIST[paramInt]+"\n");
            if(SET_DM_PORT_STATUS_LIST[paramInt].contains("adb")){
                outputStream.writeBytes("settings put global adb_enabled 1\n");
            }

I am in the process of reversing IOTHIDDENMENU.apk and recreating it's methods but without the internal and hidden api it uses.

like image 179
idone Avatar answered Sep 29 '22 12:09

idone


On a regular device, with a regular app, you can't.

You need a rooted device, with an app in /system/app, then you can.

Anyway, you shouldn't configure such a thing by yourself, the user should be in full control in such a case.

like image 28
hundeva Avatar answered Sep 29 '22 11:09

hundeva