auto-start ADB if it's not running

This commit is contained in:
adelphes
2017-02-01 11:04:40 +00:00
parent d324990e28
commit e23fc698a2
2 changed files with 39 additions and 1 deletions

View File

@@ -87,6 +87,29 @@ ADBClient.prototype = {
}); });
}, },
test_adb_connection : function(o) {
var x = {o:o||{},deferred:$.Deferred()};
this.proxy_connect()
.then(function() {
return this.dexcmd('cn');
})
.then(function(data) {
this.fd = data;
return this.dexcmd('dc', this.fd);
})
.then(function() {
return this.proxy_disconnect();
})
.then(function() {
x.deferred.resolveWith(x.o.ths||this, [null, x.o.extra]);
})
.fail(function(err) {
// if we fail, still resolve the deferred, passing the error
x.deferred.resolveWith(x.o.ths||this, [err, x.o.extra]);
});
return x.deferred;
},
list_devices : function(o) { list_devices : function(o) {
var x = {o:o||{},deferred:$.Deferred()}; var x = {o:o||{},deferred:$.Deferred()};
this.proxy_connect() this.proxy_connect()

View File

@@ -307,8 +307,23 @@ class AndroidDebugSession extends DebugSession {
if (!launchActivity) if (!launchActivity)
if (!(launchActivity = this.apk_file_info.launcher)) if (!(launchActivity = this.apk_file_info.launcher))
return fail_launch('No valid launch activity found in AndroidManifest.xml or launch.json'); return fail_launch('No valid launch activity found in AndroidManifest.xml or launch.json');
return this.findSuitableDevice(args.targetDevice);
return new ADBClient().test_adb_connection()
.then(err => {
// if adb is not running, see if we can start it ourselves using ANDROID_HOME (and a sensible port number)
var adbport = ws_proxy.adbport;
if (err && process.env.ANDROID_HOME && typeof adbport === 'number' && adbport > 0 && adbport < 65536) {
var adbpath = path.join(process.env.ANDROID_HOME, 'platform-tools', /^win/.test(process.platform)?'adb.exe':'adb');
var adbargs = ['-P',''+adbport,'start-server'];
try {
this.LOG([adbpath, ...adbargs].join(' '));
var stdout = require('child_process').execFileSync(adbpath, adbargs, {cwd:process.env.ANDROID_HOME, encoding:'utf8'});
this.LOG(stdout);
} catch (ex) {} // if we fail, it doesn't matter - the device query will fail and the user will have to work it out themselves
}
})
}) })
.then(() => this.findSuitableDevice(args.targetDevice))
.then(device => { .then(device => {
this._device = device; this._device = device;
this._device.adbclient = new ADBClient(this._device.serial); this._device.adbclient = new ADBClient(this._device.serial);