Skip to content

Commit

Permalink
connect adb over wifi
Browse files Browse the repository at this point in the history
  • Loading branch information
itsspriyansh committed Feb 28, 2024
1 parent 59c7d45 commit c665135
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/commands/android/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SdkBinary, string> = {
sdkmanager: path.join('cmdline-tools', 'latest', 'bin'),
avdmanager: path.join('cmdline-tools', 'latest', 'bin'),
Expand Down
43 changes: 32 additions & 11 deletions src/commands/android/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +26,7 @@ import {
} from './utils/sdk';

import DOWNLOADS from './downloads.json';
import {connectToWifiADB} from './utils/wireless-adb';


export class AndroidSetup {
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -521,21 +527,36 @@ export class AndroidSetup {

return;
}
}

const serverStarted = execBinarySync(
adbLocation,
'adb',
this.platform,
'start-server'
);
async startAdb(): Promise<boolean> {
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<AdbConnectionConfig> {
const res = await prompt(ADB_CONFIG_QUES);

return res;
}

verifySetup(setupConfigs: SetupConfigs): string[] {
const missingRequirements: string[] = [];

Expand Down
4 changes: 4 additions & 0 deletions src/commands/android/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
60 changes: 60 additions & 0 deletions src/commands/android/utils/wireless-adb.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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;
}
}

0 comments on commit c665135

Please sign in to comment.