Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable USB debugging (under settings/applications/development) programmatically from within an app

Is it possible to enable USB debugging (under settings/applications/development) programmatically from within my app?

I was looking at Permission.WRITE_SETTINGS and http://developer.android.com/reference/android/provider/Settings.System.html, but I couldn't find anything appropriate.

like image 939
Mathias Conradt Avatar asked Aug 11 '10 12:08

Mathias Conradt


4 Answers

First: Your app must be a system app

This line of code may help:

Settings.Global.putInt(getContentResolver(), Global.ADB_ENABLED, 1);

and you need this permission:

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

but after adding this permission in manifest you will get this error: Permission is only granted to system apps

which means your app must be a system app.

like image 173
Milad Faridnia Avatar answered Oct 19 '22 03:10

Milad Faridnia


You need to follow these 3 steps

  1. in the manifest put <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
  2. Use this command in the code to enable USB debug Settings.Secure.putInt(requireActivity().contentResolver, Settings.Global.ADB_ENABLED, 1) or Settings.Secure.putInt(requireActivity().contentResolver, Settings.Global.ADB_ENABLED, 0) to disable it
  3. IMPORTANT Before being able to use these system API you must grant WRITE_SECURE_SETTINGS from adb running adb shell pm grant your.package.name android.permission.WRITE_SECURE_SETTINGS. The device has to be attached to the computer with USB debug active.

I developed a simple app that uses this approach to enable/disable some developer options such as Debug USB and Keep Device Screen Active https://play.google.com/store/apps/details?id=it.stzan.devtools

Disclaimer: I am the actual developer of DevTools

like image 44
Stefano Zanella Avatar answered Oct 19 '22 02:10

Stefano Zanella


You will need root permissions to do so from an app.

That said, it is possible to enable ADB by executing the following terminal commands:

setprop persist.service.adb.enable 1
start adbd

This blog post gives an excellent example of how to execute these commands with escalated permissions through su.

like image 45
Paul Lammertsma Avatar answered Oct 19 '22 04:10

Paul Lammertsma


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#

like image 43
Mathias Conradt Avatar answered Oct 19 '22 04:10

Mathias Conradt