Enable USB debugging via recovery. How to properly enable USB debugging mode on Android: the most effective ways

Among users of Android-based devices, not everyone understands the intricacies of the functioning of their device. Often, someone needs to enable USB debugging, but the person does not know how to do it. In addition, the option to activate debugging often moves from one menu item to another depending on the version installed operating system and features of the software shell (firmware).

So, where should you look first?:
1. Settings-->Applications-->Development-->USB Debugging.
2. Settings-->System-->About Tablet PC-->Build Number. Next, you will need to intensively tap on the build number. This action, repeated about 10 times, leads to unlocking the settings for developers. Next you should return to main section settings and enter the menu item "For Developers" that appears. All that remains is to check the box next to the corresponding “USB Debugging” item.

Is it possible to enable USB debugging if the tablet is turned off?

Unfortunately, modern devices cannot yet please their users with this opportunity. USB debugging on a tablet can only be enabled or disabled in the settings. In a situation where the tablet cannot be started or the operating system does not load, we recommend using the function.

What to do if the device’s sensor does not work or the touchscreen and glass are broken?

When the sensor does not respond to user actions, but the screen still displays text and images, you can connect a mouse to the gadget using a regular OTG cable.
Next, by controlling the cursor, you need to go to the tablet settings, where you find the required item and activate USB Debugging. In the case where the screen also does not show anything, the only option to correct the situation is to replace the display.

It so happened that I broke the screen of my beloved Nexus 4. My first thought was “Damn!” Now I'll be like one of these beggars, with broken screen! But, apparently, the creators of the Nexus 4 were ardent opponents of poverty, since along with the broken screen, it completely failed touch screen. In general, it’s okay, take the phone in for repair and that’s it. However, there were files on the phone that I needed right now, and not in a couple of weeks. But it was only possible to get them with the screen unlocked; the phone required entering a “super secret” gesture and categorically did not want to work as an external drive.

After tinkering a little with adb, I gave up on trying to unlock the screen through the console. All the tips for hacking the lock screen required root, and my phone is not one of those. It was decided to act from within. The choice fell on the JCIFS library, since I had already worked with it before and had no problems using it.

It was necessary to write an application that would independently copy files from the phone to a folder shared via Wi-Fi. Prerequisites for such a trick: USB debugging is enabled and the computer to which the phone has already given permission for debugging, as well as the presence Wi-Fi networks, to which the phone will connect as soon as it sees it (for me this is my home Wi-Fi).

Preparatory work

Let's create a project with one Activity. Although it won’t see the light of day because of the lock screen, it will be needed to launch the service that will do the main work.

MainActivity.java

public class MainActivity extends Activity ( @Override protected void onCreate(Bundle savedInstanceState) ( super.onCreate(savedInstanceState); startService(new Intent(this, SynchronizeService.class)); ) )


A separate service will handle copying files. Since the Activity is not visible, you shouldn’t count on its viability, but the service launched in Foreground will cope with this task perfectly.

SynchronizeService.java

public class SynchronizeService extends Service ( private static final int FOREGROUND_NOTIFY_ID = 1; @Override public void onCreate() ( super.onCreate(); final NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.synchronize_service_message)) .setContentIntent(getDummyContentIntent()) .setColor(Color.BLUE) .setProgress(1000, 0, true); startForeground( FOREGROUND_NOTIFY_ID, builder.build()); // This will help keep the CPU awake PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); final WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SynchronizeWakelockTag"); wakeLock.acquire( ); ) @Override public int onStartCommand(Intent intent, int flags, int startId) ( return START_NOT_STICKY; ) @Override public IBinder onBind(Intent intent) ( return null; ) )


Before we move on, let's add a dependency, a build.gradle file, that will add the JCIFS library to the project.
dependencies ( ... compile "jcifs:jcifs:1.3.17" )
You also need to add some permissions to the manifest and don’t forget to write about our service there. Ultimately, my AndroidManifest.xml looked like this.

AndroidManifest.xml

// Needed to keep the phone from sleeping. // Required for working with the network. // For reading from SD card.

Copying files

So, all preparations are completed. Now, if you run the application, service messages will appear in the list of notifications (starting from Android 5, you can configure the display of messages on the lock screen. If the Android version is lower, you will not see this message), which means that the application works as it should and you can start the most delicious thing - transferring files.

In order not to perform network operations in the main thread, we’ll move this whole thing into an AsyncTask.
public class CopyFilesToSharedFolderTask extends AsyncTask ( private final File mFolderToCopy; private final String mSharedFolderUrl; private final NtlmPasswordAuthentication mAuth; private FileFilter mFileFilter; public CopyFilesToSharedFolderTask(File folderToCopy, String sharedFolderUrl, String user, String password, FileFilter fileFilter) ( super(); mFolderToCopy = folderToCopy; // Folder , which should be copied. mSharedFolderUrl = sharedFolderUrl; // Url to the network folder to which files from the phone will be copied. mAuth = (user != null && password != null) ? new NtlmPasswordAuthentication(user + ":" + password) : NtlmPasswordAuthentication.ANONYMOUS; mFileFilter = fileFilter; ) ) Particular attention should be paid to the user and password parameters. This is the login and password for the network folder that will be used to create NtlmPasswordAuthentication. If a password is not required to access the folder, you must use NtlmPasswordAuthentication.ANONYMOUS as authentication. While it looks simple, authentication is the biggest problem you'll encounter when working with network shares. Typically, most problems are hidden in incorrectly configured security policies on the computer. The best way to check that the settings are correct is to try opening a network folder on your phone using any other file manager that supports working over the network.

SmbFile is a file for working with network files. Surprisingly, JCIFS is very easy to work with files. You will feel almost no difference between SmbFile and regular File. The only thing that catches your eye is the presence of managed exceptions in almost all methods of the class. And to create the SmbFile object, you will need the authentication data that we created earlier.
private double mMaxProgress; private double mProgress; ... @Override protected String doInBackground(Void... voids) ( mMaxProgress = getFilesSize(mFolderToCopy); mProgress = 0; publishProgress(0d); try ( SmbFile sharedFolder = new SmbFile(mSharedFolderUrl, mAuth); if (sharedFolder.exists () && sharedFolder.isDirectory()) ( copyFiles(mFolderToCopy, sharedFolder); ) ) catch (MalformedURLException e) ( return "Invalid URL."; ) catch (IOException e) ( e.printStackTrace(); return e.getMessage( ); ) return null; ) The doInBackground method returns an error message. If null is returned, then everything went smoothly and without errors.

There can be a lot of files... No, not like that. There can be a LOT of them! Therefore, showing progress is a vital function. The recursive getFilesSize method calculates the total size of files that will be needed to calculate the total progress.
private double getFilesSize(File file) ( if (!checkFilter(file)) return 0; if (file.isDirectory()) ( int size = 0; File filesList = file.listFiles(); for (File innerFile: filesList) size += getFilesSize(innerFile); return size; ) return (double) file.length(); ) private boolean checkFilter(File file) ( return mFileFilter == null || mFileFilter.accept(file); ) The filter passed to the constructor helps exclude unnecessary files and folders. For example, you can exclude all folders starting with a dot or add the “Android” folder to the blacklist.

As I said earlier, working with SmbFile is no different from working with a regular file, therefore, the process of transferring data from a phone to a computer is not original. I’ll even hide this code under a spoiler so as not to clutter the article with even more obvious code.

The copyFiles and copySingleFile methods

private static final String LOG_TAG = "WiFiSynchronizer"; private void copyFiles(File fileToCopy, SmbFile sharedFolder) throws IOException ( if (!checkFilter(fileToCopy)) return; // If the file or folder does not pass the filter, do not copy it. if (fileToCopy.exists()) ( if (fileToCopy. isDirectory()) ( File filesList = fileToCopy.listFiles(); // When creating a directory, put "/" at the end. SmbFile newSharedFolder = new SmbFile(sharedFolder, fileToCopy.getName() + "/"); if (!newSharedFolder. exists()) ( newSharedFolder.mkdir(); Log.d(LOG_TAG, "Folder created:" + newSharedFolder.getPath()); ) else Log.d(LOG_TAG, "Folder already exist:" + newSharedFolder.getPath() ); for (File file: filesList) copyFiles(file, newSharedFolder); // Recursive call ) else ( SmbFile newSharedFile = new SmbFile(sharedFolder, fileToCopy.getName()); // If the file has already been created, we will not copy it. // Of course, in another situation, it would be worth adding a hash check, but in my case it would be unnecessary if (!newSharedFile.exists()) ( copySingleFile(fileToCopy, newSharedFile); Log.d(LOG_TAG, "File copied:" + newSharedFile.getPath()); ) else Log.d(LOG_TAG, "File already exists:" + newSharedFile.getPath()); // Update progress. mProgress += (double) fileToCopy.length(); publishProgress(mProgress / mMaxProgress * 100d); ) ) ) // An unremarkable method for copying files. private void copySingleFile(File file, SmbFile sharedFile) throws IOException ( IOException exception = null; InputStream inputStream = null; OutputStream outputStream = null; try ( outputStream = new SmbFileOutputStream(sharedFile); inputStream = new FileInputStream(file); byte bytesBuffer = new byte; int bytesRead; while ((bytesRead = inputStream.read(bytesBuffer)) > 0) ( outputStream.write(bytesBuffer, 0, bytesRead); ) ) catch (IOException e) ( exception = e; ) finally ( if (inputStream != null) try ( inputStream.close(); ) catch (IOException e) ( e.printStackTrace(); ) if (outputStream != null) try ( outputStream.close(); ) catch (IOException e) ( e. printStackTrace(); ) ) if (exception != null) throw exception; )

The code is obvious, but there is one, not at all obvious, point in it - adding the character "/" to the end of the folder name when creating a new SmbFile. The fact is that JCIFS treats all files that do not end with the "/" character only as a file, not as a directory. Therefore, if the Url of a network folder looks like this: “file://MY-PC/shared/some_foldel”, incidents will arise when creating a new folder in the “some_foldel” folder. Namely, "some_foldel" will be discarded, and the new folder will have Url: "file://MY-PC/shared/new_folder", instead of the expected "file://MY-PC/shared/some_foldel/new_folder". At the same time, for such folders, the isDirectory, mkdir or listFiles methods will work correctly.

Almost done. Now let's start executing this task in onCreate of the service.
private static final int FOREGROUND_NOTIFY_ID = 1; private static final int MESSAGE_NOTIFY_ID = 2; private static final String SHARED_FOLDER_URL = "file://192.168.0.5/shared/"; ... final File folderToCopy = getFolderToCopy(); CopyFilesToSharedFolderTask task = new CopyFilesToSharedFolderTask(folderToCopy, SHARED_FOLDER_URL, null, null, null) ( @Override protected void onProgressUpdate(Double... values) ( builder.setProgress(100, values.intValue(), false) .setContentText(String.format ("%s %.3f", getString(R.string.synchronize_service_progress), values) + "%"); mNotifyManager.notify(FOREGROUND_NOTIFY_ID, builder.build()); ) @Override protected void onPostExecute(String errorMessage) ( stopForeground(true); if (errorMessage == null) showNotification(getString(R.string.synchronize_service_success), Color.GREEN); else showNotification(errorMessage, Color.RED); stopSelf(); wakeLock.release(); // Don't forget to release wakeLock ) @Override protected void onCancelled(String errorMessage) ( // This code will never be executed. But you never know, maybe I want to change something. // Then the service will never stop when the task is closed. stopSelf(); wakeLock.release(); ) ); task.execute(); In my case, a login and password are not required, I also did not specify a filter. The onProgressUpdate method is overridden to show the progress status, and onPostExecute shows a message about the completion of the download, or about an error, and then terminates the service.

Let's launch the application. A message appeared from the running service. While the total file size is being calculated, an indeterminate progress status is shown. But the indicator shows 0%, after which the bar gradually, in small, barely noticeable steps, begins to move towards 100%.



When the work was completed, a message indicating a successful result was displayed on the screen, and on my computer there were all the necessary files that had previously been stored on the broken phone.

Unexpected conclusions

I got what I needed. Now is the time to brew some tea, lounge on the sofa and turn on some TV series. But wait! Despite the fact that the phone was mine and access to the files on it does not contradict Russian law, I took them out without using a password! At the same time, the phone was not Rooted. This means that with just the debugging mode turned on, it is not difficult to access the contents of the “SD card” without even knowing the password. The only saving grace is that the only weak spot in protection against hacking is the need to use a computer that already has debugging rights.

The new version of Android, introduced not long ago, may close this hole, since accessing the necessary permissions will require user confirmation, which is impossible when the screen is locked. In the meantime, Android developer, be on your guard if you don’t want someone else to see your nude photos. And remember, by allowing any computer to debug via USB, you are creating another loophole for hacking your own phone.

Thank you for your attention. I'd love to see your thoughts in the comments.
The source code can be found at the following link: github.com/KamiSempai/WiFiFolderSynchronizer

UPD: As I suspected, there is a simpler solution to my problem. Namely, take advantage adb command pull. Unfortunately, the rare use of adb and a narrow view of the problem did not allow me to come to it. I tried to unlock the screen, not download files.
Thanks to EvgenT for the good remark.

In some situations, it becomes necessary to diagnose a smartphone via a PC. For this purpose, the developers have created a special USB debugging mode. Despite the fact that this option is always in the menu system settings, she may have many options for her further location. Let's look at a few basic ways to enable USB debugging mode on Android.

Device menu

Debugging setup


The entrance to the menu can be either open or hidden. This depends on the brand of your device and the version of the Android operating system and remains at the discretion of the developers. If it is open, then test it mobile device It will be easiest through a computer. Depending on the model, the input may be located in different tabs of the system settings menu. We list the main options for its location:
  • The “for developers” tab (“developer menu”, “developer options”), which is located directly in the main settings menu.
  • A subsection of one of the items: “special features”, “auxiliary settings”, etc.

brief information

Enabling the mode


After you go to the desired directory, the USB debugging item will be the first in the list presented. To enable it, you need to click the checkbox located opposite the item name. Now you can connect your mobile device to your computer and work with it using the ADB utility. We recommend watching a visual video instruction on how to enable USB debugging mode on Android version 5.1.

Login with hidden menu

Device settings

Device characteristics


In many smartphone models running the Android 4.2 operating system and higher, the mode is hidden from users. You can detect and enable it in the following way:
  • Go to the system settings menu.
  • Select the “About device” tab.
  • Quickly tap on the “build number” item until the system issues an information notification that you have been assigned developer status.

After this, you will be able to use the special settings menu of your Android smartphone. To enable USB debugging mode, you must repeat the steps described in the previous section.

Software details

Software properties

Through Recovery mode

In some cases, it is not possible to enable this mode on a mobile device. This could be, for example, due to damage to the touchscreen screen or a non-functioning mobile phone in general. In such a situation, a method that involves using recovery mode will help you. It is worth noting that to implement this, your smartphone must have custom Recovery CWM or TWRP. This will allow you to get root rights access to the mobile phone operating system.
Let's consider the list of necessary actions that need to be performed to be able to debug the phone via USB in this case:

  • From the official website, download and unpack the qtADB-cwm-edition application into the root directory of the computer operating system.
  • There, create a folder called SQlite3_Windows and install the pre-downloaded SQLite relational database into it.
  • Reboot your mobile phone and log in Recovery Mode by simultaneously pressing several keys. How to enter recovery mode specific model can be read on the manufacturer's official website.
  • Connect your mobile phone to your computer using USB cable.
  • Run the executive file QtADB.exe on your computer, located in the folder of the manager you downloaded earlier.
  • Click the “advanced” item and make sure that the “Data” section of the mobile device is displayed correctly.
  • Select the “files” tab in the left panel, and then open the previously created folder with the SQLite relational database.
  • Open the directory data/com.android.providers.settings/databases/ in the right panel of the program.
  • Move the file called settings.db from the right panel folder to the left one.
  • Run command Windows string(using the Win+R key combination) and enter: “cd C:\Sqlite3_Windows”, “sqlite3 settings.db”, “update secure set value=1 where name=’adb_enabled”.
  • We copy back the file called settings.db, agreeing to rewrite and reboot the mobile device.

If none of the methods work

Let's consider what needs to be done if ADB does not see the mobile device. First of all, you need to check the software component, which may involve using unofficial firmware or inappropriate ADB drivers. The OS version can always be downloaded from the manufacturer’s website, and the required software from Google website. Another reason why debugging from a computer does not work is the use of a damaged USB cable or connector. You can check this by connecting your mobile phone using a different wire and into a different connector.