Skip to content

Commit

Permalink
added connect adb wirelessly functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
itsspriyansh committed Mar 1, 2024
1 parent 59c7d45 commit 2fba69a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/commands/android/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
}
};

Expand Down
7 changes: 7 additions & 0 deletions src/commands/android/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from './utils/sdk';

import DOWNLOADS from './downloads.json';
import {connectAdbWirelessly} from './utils/adbWirelessConnect';


export class AndroidSetup {
Expand Down Expand Up @@ -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);
Expand Down
74 changes: 74 additions & 0 deletions src/commands/android/utils/adbWirelessConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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<boolean> {
try {
const deviceList = execBinarySync(adbLocation, 'adb', platform, 'devices -l');
if (deviceList !== null) {
if (!deviceList.toString().includes('transport_id')) {
Logger.log(colors.red('No devices found. Please connect a device and try again.'));

return false;
}
}

Logger.log(`
${colors.bold('Follow the below steps to connect to your device wirelessly:\n')}
1. Enable USB debugging on your device.
2. Connect your device to your computer using a USB cable.
3. Grant permission for File transfer on your device.
4. Connect your device to the same network as your computer ${colors.grey('(you may connect your device to your computer\'s hotspot)')}.
5. Enable wireless debugging on your device.
6. Find the IP address, port number and pairing code of your device by going to
${colors.cyan('Settings > Wireless Debugging > Pair device with pairing code.')}\n`);

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('\nPairing successful!')} ${colors.bold('Please disconnect your device before proceeding.\n')}`);
} else {
Logger.log(`${colors.red('\nPairing failed!')} please try again.`);

return false;
}

const finalPortAnswer = await inquirer.prompt({
type: 'input',
name: 'finalPort',
message: 'Enter new port number after disconnecting usb:'
});
const finalPort = finalPortAnswer.finalPort;

execBinarySync(adbLocation, 'adb', platform, `connect ${deviceIP}:${finalPort}`);
Logger.log(colors.green('\nConnected to device wirelessly.'));

return true;
} catch (error) {
Logger.log('Error connecting to wifi ADB');
console.error('Error:', error);

return false;
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 2fba69a

Please sign in to comment.