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..77f922b 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,8 @@ export class AndroidSetup { result = await this.verifyAndSetupBrowsers(setupConfigs.browsers); } + await this.startAdb(); + this.postSetupInstructions(result, setupConfigs); if (setupConfigs.mode !== 'emulator') { @@ -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 { + 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..8264704 --- /dev/null +++ b/src/commands/android/utils/wireless-adb.ts @@ -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'); + } +}