From c665135263d60a6a5089b59e63524c375a676b9a Mon Sep 17 00:00:00 2001 From: itsspriyansh Date: Tue, 27 Feb 2024 21:29:17 +0530 Subject: [PATCH] connect adb over wifi --- src/commands/android/constants.ts | 12 +++++ src/commands/android/index.ts | 43 ++++++++++++---- src/commands/android/interfaces.ts | 4 ++ src/commands/android/utils/wireless-adb.ts | 60 ++++++++++++++++++++++ 4 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 src/commands/android/utils/wireless-adb.ts diff --git a/src/commands/android/constants.ts b/src/commands/android/constants.ts index 513b174..138bb43 100644 --- a/src/commands/android/constants.ts +++ b/src/commands/android/constants.ts @@ -75,6 +75,18 @@ export const SETUP_CONFIG_QUES: inquirer.QuestionCollection = [ } ]; +export const ADB_CONFIG_QUES: inquirer.QuestionCollection = [ + { + type: 'list', + name: 'mode', + message: 'How do you want to connect the device?', + choices: [ + {name: 'With USB cable', value: 'usb'}, + {name: 'Over WiFi', value: 'wifi'} + ] + } +]; + export const SDK_BINARY_LOCATIONS: Record = { sdkmanager: path.join('cmdline-tools', 'latest', 'bin'), avdmanager: path.join('cmdline-tools', 'latest', 'bin'), diff --git a/src/commands/android/index.ts b/src/commands/android/index.ts index f1d5327..4b3518f 100644 --- a/src/commands/android/index.ts +++ b/src/commands/android/index.ts @@ -12,10 +12,10 @@ import Logger from '../../logger'; import {getPlatformName, symbols} from '../../utils'; import {getAlreadyRunningAvd, killEmulatorWithoutWait, launchAVD} from './adb'; import { - ABI, AVAILABLE_OPTIONS, BINARY_TO_PACKAGE_NAME, DEFAULT_CHROME_VERSIONS, + ABI, ADB_CONFIG_QUES, AVAILABLE_OPTIONS, BINARY_TO_PACKAGE_NAME, DEFAULT_CHROME_VERSIONS, DEFAULT_FIREFOX_VERSION, NIGHTWATCH_AVD, SETUP_CONFIG_QUES } from './constants'; -import {AndroidSetupResult, Options, OtherInfo, Platform, SdkBinary, SetupConfigs} from './interfaces'; +import {AdbConnectionConfig, AndroidSetupResult, Options, OtherInfo, Platform, SdkBinary, SetupConfigs} from './interfaces'; import { downloadFirefoxAndroid, downloadWithProgressBar, getAllAvailableOptions, getBinaryLocation, getBinaryNameForOS, getFirefoxApkName, getLatestVersion @@ -26,6 +26,7 @@ import { } from './utils/sdk'; import DOWNLOADS from './downloads.json'; +import {connectToWifiADB} from './utils/wireless-adb'; export class AndroidSetup { @@ -133,6 +134,11 @@ export class AndroidSetup { result = await this.verifyAndSetupBrowsers(setupConfigs.browsers); } + const isAdbRunning = await this.startAdb(); + if (!isAdbRunning) { + return false; + } + this.postSetupInstructions(result, setupConfigs); if (setupConfigs.mode !== 'emulator') { @@ -521,21 +527,36 @@ export class AndroidSetup { return; } + } - const serverStarted = execBinarySync( - adbLocation, - 'adb', - this.platform, - 'start-server' - ); + async startAdb(): Promise { + const {mode} = await this.getAdbConnectionConfig(); + const adbLocation = getBinaryLocation(this.sdkRoot, this.platform, 'adb', true); + if (mode==='usb') { + const serverStarted = execBinarySync( + adbLocation, + 'adb', + this.platform, + 'start-server' + ); + if (serverStarted !== null) { + Logger.log(`${colors.green('Success!')} adb server is running.\n`); + } else { + Logger.log('Please try running the above command by yourself.\n'); + } - if (serverStarted !== null) { - Logger.log(`${colors.green('Success!')} adb server is running.\n`); + return true; } else { - Logger.log('Please try running the above command by yourself.\n'); + return await connectToWifiADB(adbLocation, this.platform); } } + async getAdbConnectionConfig(): Promise { + const res = await prompt(ADB_CONFIG_QUES); + + return res; + } + verifySetup(setupConfigs: SetupConfigs): string[] { const missingRequirements: string[] = []; diff --git a/src/commands/android/interfaces.ts b/src/commands/android/interfaces.ts index a64b66c..66b10df 100644 --- a/src/commands/android/interfaces.ts +++ b/src/commands/android/interfaces.ts @@ -27,4 +27,8 @@ export interface SetupConfigs { browsers?: 'chrome' | 'firefox' | 'both' | 'none'; } +export interface AdbConnectionConfig { + mode?: 'wifi' | 'usb'; +} + export type SdkBinary = 'sdkmanager' | 'adb' | 'emulator' | 'avdmanager'; diff --git a/src/commands/android/utils/wireless-adb.ts b/src/commands/android/utils/wireless-adb.ts new file mode 100644 index 0000000..7c6e55f --- /dev/null +++ b/src/commands/android/utils/wireless-adb.ts @@ -0,0 +1,60 @@ +import {Platform} from '../interfaces'; +import {execBinarySync} from './sdk'; +import Logger from '../../../logger'; +import inquirer from 'inquirer'; + +export async function connectToWifiADB(adbLocation: string, platform: Platform): Promise { + try { + const deviceList = execBinarySync(adbLocation, 'adb', platform, 'devices -l'); + if (deviceList !== null) { + if (!deviceList.toString().includes('transport_id')) { + Logger.log('No devices found. Please connect a device and try again.'); + + return false; + } + } + + const deviceIPAnswer = await inquirer.prompt({ + type: 'input', + name: 'deviceIP', + message: 'Enter the IP address of your device:' + }); + const deviceIP = deviceIPAnswer.deviceIP; + + const portAnswer = await inquirer.prompt({ + type: 'input', + name: 'port', + message: 'Enter the desired port number:' + }); + const port = portAnswer.port; + + const pairingCodeAnswer = await inquirer.prompt({ + type: 'input', + name: 'pairingCode', + message: 'Enter the pairing code displayed on your device:' + }); + const pairingCode = pairingCodeAnswer.pairingCode; + + const pairing = execBinarySync(adbLocation, 'adb', platform, `pair ${deviceIP}:${port} ${pairingCode}`); + if (pairing !== null) { + Logger.log(pairing.toString()); + } + + const finalPortAnswer = await inquirer.prompt({ + type: 'input', + name: 'finalPort', + message: 'Enter port number to connect:' + }); + const finalPort = finalPortAnswer.finalPort; + + execBinarySync(adbLocation, 'adb', platform, `connect ${deviceIP}:${finalPort}`); + Logger.log('Connected to device wirelessly.'); + + return true; + } catch (error) { + Logger.log('Error connecting to wifi ADB'); + console.error('Error:', error); + + return false; + } +}