12

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

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
User10001
  • 1,295
  • 2
  • 18
  • 30
  • All the answers below are half-correct. In order to programatically enable/disable usb debugging, you'll need to sign your app with an Android platform (Gingerbread, Jellybean, etc) certificate, AND your app has the appropriate configs in AndroidManifest.xml, AND your app is installed in the /system/app directory programmatically or manually. You cannot do this alone with a rooted device by simply moving it to the /system/app directory. It doesn't work like that. – ChuongPham Dec 11 '13 at 04:01

5 Answers5

6

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.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
idone
  • 61
  • 1
  • 1
3

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.

hundeva
  • 1,058
  • 14
  • 30
  • Settings.Secure provides the method to write system settings but it is not working. please find the below method Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.ADB_ENABLED, 0); – User10001 Sep 13 '13 at 12:09
2

This is not possible in android because for that you have to access Setting.System. For more info take a look here : How can I disable Android USB debugging programmatically

Community
  • 1
  • 1
Harish Godara
  • 2,388
  • 1
  • 14
  • 28
  • Settings.Secure provides the method to write system settings but it is not working. please find the below method Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.ADB_ENABLED, 0); – User10001 Sep 13 '13 at 12:06
2

It's not possible without using your own custom firmware that grants access to the security settings. See thread here: http://groups.google.com/group/android-developers/browse_frm/thread/953c6f0eb0fa9bed#

usb debugging is another name for the Android Debug Bridge (ADB). The item you're looking for is here

http://developer.android.com/reference/android/provider/Settings.Secure.html#ADB_ENABLED

Anchit Mittal
  • 3,412
  • 4
  • 28
  • 48
-3

great answer : https://stackoverflow.com/a/17029123/1136074

basicly its:

android.os.Debug.waitForDebugger(); also you can use the following to determine if the debugger is connected:

android.os.Debug.isDebuggerConnected(); //Determine if a debugger is currently att
Community
  • 1
  • 1
gor
  • 1,046
  • 1
  • 14
  • 28