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 27, 2024
1 parent 59c7d45 commit 9c4ffcc
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 12 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
40 changes: 28 additions & 12 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,8 @@ export class AndroidSetup {
result = await this.verifyAndSetupBrowsers(setupConfigs.browsers);
}

await this.startAdb();

this.postSetupInstructions(result, setupConfigs);

if (setupConfigs.mode !== 'emulator') {
Expand Down Expand Up @@ -521,21 +524,34 @@ export class AndroidSetup {

return;
}
}

const serverStarted = execBinarySync(
adbLocation,
'adb',
this.platform,
'start-server'
);

if (serverStarted !== null) {
Logger.log(`${colors.green('Success!')} adb server is running.\n`);
async startAdb() {
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');
}
} else {
Logger.log('Please try running the above command by yourself.\n');
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';
49 changes: 49 additions & 0 deletions src/commands/android/utils/wireless-adb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {Platform} from '../interfaces';
import {execBinarySync} from './sdk';
import Logger from '../../../logger';

export async function connectToWifiADB(adbLocation: string, platform: Platform) {
try {
const devicesOutput = execBinarySync(adbLocation, 'adb', platform, 'devices');
if (devicesOutput === null) {

return;
}
if (!devicesOutput.includes('attached')) {
Logger.log('No devices connected. Please connect your device using a USB cable.');

return;
}

const ipAddressOutput = execBinarySync(adbLocation, 'adb', platform, 'shell ifconfig wlan0');
if (ipAddressOutput === null) {

return;
}
const ipAddressRegExp = /inet addr:([^ ]+)/;
const match = ipAddressOutput.match(ipAddressRegExp);
if (!match) {
Logger.log('Failed to retrieve device IP address.');

return;
}
const deviceIP = match[1];

execBinarySync(adbLocation, 'adb', platform, 'tcpip 5555');

execBinarySync(adbLocation, 'adb', platform, `connect ${deviceIP}:5555`);

const connectedDevicesOutput = execBinarySync(adbLocation, 'adb', platform, 'devices');
if (connectedDevicesOutput === null) {

return;
}
if (connectedDevicesOutput.includes(`${deviceIP}:5555`)) {
Logger.log('Device successfully connected wirelessly.');
} else {
Logger.log('Failed to connect to device wirelessly.');
}
} catch (error) {
Logger.log('Error connecting to wifi ADB');
}
}

0 comments on commit 9c4ffcc

Please sign in to comment.