Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking to enable and disable (toggle) ADB or USB debugging using command line or in app

Tags:

android

adb

I'm trying to enable ADB (USB debugging) only when my application is running and disabling it when my application is not. I have full access to the phone and it is rooted, su available, etc., but I cannot find a way to do the toggling.

What I've tried so far:

Process proc = Runtime.getRuntime().exec(new String [] { "su", "-c", "setprop", "persist.service.adb.enable", "0"});
proc.waitFor();
Process proc2 = Runtime.getRuntime().exec(new String [] { "su", "-c", "stop", "adbd"});
proc2.waitFor();

This however causes the phone to enter a reboot loop instantaneously.

like image 830
Chris Avatar asked Nov 30 '22 02:11

Chris


2 Answers

The code that works is easy:

Settings.Secure.putInt(getContentResolver(),Settings.Secure.ADB_ENABLED, 0); // 0 to disable, 1 to enable

Edit/Update: Thanks to @MohanT for the following update:

Settings.Global.putInt(getContentResolver(),Settings.Global.ADB_ENABLED, 0); // 0 to disable, 1 to enable

However, the app needs to be a system app to be able to use this. To get out of having to sign it, build a custom rom, etc.. I did the following to get it to be a system app.

First, I installed it regularly using eclipse, then adb shell:

> su
# mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
# cat /data/app/filename.apk > /system/app/filename.apk
# mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system
# reboot
like image 87
Chris Avatar answered Dec 04 '22 20:12

Chris


You can achieve by following next steps:

  1. Check whether ADB is currently enabled. To do that you can use a relevant global constant:

    • Settings.Global.ADB_ENABLED for API 17 and above.
    • Settings.Secure.ADB_ENABLED prior to API 17.
  2. Use implicit Intent with Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS action to open developers options, where user can enable or disable ADB.

Code example:

Java

public static final int API = Build.VERSION.SDK_INT;
public static final int ENABLED=1, DISABLED=0;

public static boolean adbEnabled(Context context){
    if(API>16)
       return ENABLED == Settings.Global.getInt(context.getContentResolver(), Settings.Global.ADB_ENABLED,DISABLED);
    else 
       return ENABLED == Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ADB_ENABLED,DISABLED);
}

public void checkAdb(Context context){
    //if ADB disabled
    if(!adbEnabled(context)){
        //open developer options settings 
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
        context.startActivity(intent);
    }
}

Kotlin

val API = Build.VERSION.SDK_INT
val ENABLED = 1
val DISABLED = 0

fun adbEnabled(context: Context): Boolean {
    return if (API > 16)
        ENABLED == Settings.Global.getInt(context.contentResolver, Settings.Global.ADB_ENABLED, DISABLED)
    else
        ENABLED == Settings.Secure.getInt(context.contentResolver, Settings.Secure.ADB_ENABLED, DISABLED)
}
fun checkAdb(context: Context) {
    //if ADB disabled
    if (!adbEnabled(context)) {
        //open developer options settings
        val intent = Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS)
        context.startActivity(intent)
    }
}

Just for reference:

Prior to API 3 (probably no longer relevant) Settings.System.ADB_ENABLED was used.

like image 27
Nikita Kurtin Avatar answered Dec 04 '22 20:12

Nikita Kurtin