From 9228731782ecab43747eae4cecd53ab30584c547 Mon Sep 17 00:00:00 2001 From: itsspriyansh Date: Fri, 1 Mar 2024 02:16:02 +0530 Subject: [PATCH] added connect adb wirelessly functionality --- src/commands/android/constants.ts | 4 ++ src/commands/android/index.ts | 7 ++ .../android/utils/adbWirelessConnect.ts | 65 +++++++++++++++++++ src/index.ts | 2 +- 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/commands/android/utils/adbWirelessConnect.ts diff --git a/src/commands/android/constants.ts b/src/commands/android/constants.ts index 513b174..7ffac67 100644 --- a/src/commands/android/constants.ts +++ b/src/commands/android/constants.ts @@ -24,6 +24,10 @@ export const AVAILABLE_OPTIONS: AvailableOptions = { appium: { alias: [], description: 'Make sure the final setup works with Appium out-of-the-box.' + }, + 'wireless-connection': { + alias: [], + description: 'Connect ADB with wireless connection.' } }; diff --git a/src/commands/android/index.ts b/src/commands/android/index.ts index f1d5327..24cf02d 100644 --- a/src/commands/android/index.ts +++ b/src/commands/android/index.ts @@ -26,6 +26,7 @@ import { } from './utils/sdk'; import DOWNLOADS from './downloads.json'; +import {connectAdbWirelessly} from './utils/adbWirelessConnect'; export class AndroidSetup { @@ -107,6 +108,12 @@ export class AndroidSetup { this.sdkRoot = sdkRootEnv || await this.getSdkRootFromUser(); process.env.ANDROID_HOME = this.sdkRoot; + if (this.options['wireless-connection']) { + const adbLocation = getBinaryLocation(this.sdkRoot, this.platform, 'adb', true); + + return await connectAdbWirelessly(adbLocation, this.platform); + } + let result = true; const setupConfigs: SetupConfigs = await this.getSetupConfigs(this.options); diff --git a/src/commands/android/utils/adbWirelessConnect.ts b/src/commands/android/utils/adbWirelessConnect.ts new file mode 100644 index 0000000..83ce95c --- /dev/null +++ b/src/commands/android/utils/adbWirelessConnect.ts @@ -0,0 +1,65 @@ +import {Platform} from '../interfaces'; +import {execBinarySync} from './sdk'; +import Logger from '../../../logger'; +import inquirer from 'inquirer'; +import colors from 'ansi-colors'; + +export async function connectAdbWirelessly(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 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) { + Logger.log(`${colors.green('Pairing successful!')} please disconnect your device before proceeding.`); + } else { + Logger.log(`${colors.red('Pairing failed!')} please try again.`); + + return false; + } + + const finalPortAnswer = await inquirer.prompt({ + type: 'input', + name: 'finalPort', + message: 'New port number after disconnecting usb:' + }); + 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; + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index c15ec74..e6592df 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ export const run = () => { try { const argv = process.argv.slice(2); const {_: args, ...options} = minimist(argv, { - boolean: ['install', 'setup', 'help', 'appium'], + boolean: ['install', 'setup', 'help', 'appium', 'wireless-connection'], alias: { help: 'h', mode: 'm',