{"record":{"id":"c41b2345b261b380","repo":"MuntashirAkon/AppManager","slug":"could-not-connect-to-adb","errorCode":null,"errorMessage":"Could not connect to ADB.","messagePattern":"Could not connect to ADB\\.","errorType":"exception","errorClass":"java.io.IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/AppManager/servermanager/LocalServerManager.java","lineNumber":281,"sourceCode":"            String command = ServerConfig.getServerRunnerCommand();\n            Log.d(TAG, \"useAdbStartServer: %s\", command);\n            executeAdbCommand(is, os, command, \"Success!\", 1, TimeUnit.MINUTES);\n        }\n        Log.d(TAG, \"useAdbStartServer: Server has started.\");\n    }\n\n    @WorkerThread\n    @NonNull\n    private AdbStream openAdbShell() throws Exception {\n        String adbHost = ServerConfig.getAdbHost(mContext);\n        int adbPort = ServerConfig.getAdbPort();\n        AdbConnectionManager manager = AdbConnectionManager.getInstance();\n        Log.d(TAG, \"Connecting to ADB using host=%s, port=%d\", adbHost, adbPort);\n        manager.setTimeout(10, TimeUnit.SECONDS);\n        // Wireless debugging can select a different port when it is re-enabled.\n        manager.disconnect();\n        if (!manager.connect(adbHost, adbPort)) {\n            throw new IOException(\"Could not connect to ADB.\");\n        }\n        Log.d(TAG, \"Opening ADB shell...\");\n        return manager.openStream(\"shell:\");\n    }\n\n    @WorkerThread\n    static void executeAdbCommand(@NonNull InputStream inputStream,\n                                  @NonNull OutputStream outputStream,\n                                  @NonNull String command,\n                                  @NonNull String successPrefix,\n                                  long timeout,\n                                  @NonNull TimeUnit timeoutUnit) throws IOException, InterruptedException {\n        CountDownLatch commandWatcher = new CountDownLatch(1);\n        AtomicBoolean commandSucceeded = new AtomicBoolean(false);\n        AtomicReference<Throwable> readFailure = new AtomicReference<>();\n        Thread outputThread = new Thread(() -> {\n            try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {\n                String response;","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/AppManager/servermanager/LocalServerManager.java#L263-L299","documentation":"LocalServerManager.openAdbShell attempts an ADB connection to the local ADB daemon (wireless debugging) and throws IOException when AdbConnectionManager.connect() fails. This means the app could not establish a socket connection to adbd, which is required to start or stop the local server over ADB.","triggerScenarios":"Calling useAdbStartServer or stopServer when the ADB daemon is unreachable: wireless debugging disabled, wrong adbHost/adbPort, daemon restarted with a new port, or connect() failing after a 10s timeout.","commonSituations":"Wireless debugging toggled off; phone rebooted so adbd picked a different pairing/connect port; developer options disabled; firewall or VPN blocking the local socket; stale saved ADB port in app settings.","solutions":["Re-enable Wireless debugging in Developer options and re-check the current port shown there","Update the ADB host/port in the app's server settings to the newly advertised port","Re-pair the app with the device (pairing code) if pairing was invalidated","Verify adbd is listening (adb devices from a PC) and that developer options/USB debugging is active","Retry the operation, as the code disconnects stale sessions before connecting"],"exampleFix":"// before\nmanager.connect(adbHost, adbPort);\n// after\nif (!manager.connect(adbHost, adbPort)) {\n    // prompt user to re-enable wireless debugging / refresh port\n    throw new IOException(\"Could not connect to ADB at \" + adbHost + \":\" + adbPort + \". Re-enable Wireless debugging and update the port.\");\n}","handlingStrategy":"retry","validationCode":"// check wireless debugging state and port before connecting\nif (!isWirelessDebuggingEnabled(context)) {\n    throw new IOException(\"Wireless debugging is disabled.\");\n}\nint currentPort = getLatestAdbPort(); // refresh, port changes on re-enable","typeGuard":null,"tryCatchPattern":"try {\n    shell = openAdbShell();\n} catch (IOException e) {\n    // prompt user to re-enable wireless debugging / refresh port, then retry once\n}","preventionTips":["Save the ADB port at pairing time and refresh it whenever wireless debugging is re-enabled","Detect and surface adbd restarts (port changes) to the user","Retry connection with backoff before failing","Validate host/port settings before attempting a connection"],"tags":["adb","network","android","connection"],"backgroundTag":"connection-refused","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}