welsonjs/lib/adb.js

96 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-03-01 18:15:24 +00:00
////////////////////////////////////////////////////////////////////////
2022-03-01 18:17:42 +00:00
// Android Debug Bridge API
2022-03-01 18:15:24 +00:00
///////////////////////////////////////////////////////////////////////
var SHELL = require("lib/shell");
var SYS = require("lib/system");
function ADBObject() {
2022-03-01 20:55:38 +00:00
this.binPath = "bin\\platform-tools_r33.0.0-windows\\platform-tools\\adb.exe";
2022-03-01 18:15:24 +00:00
this.setBinPath = function(binPath) {
this.binPath = binPath;
2022-03-01 18:58:51 +00:00
return this;
2022-03-01 18:15:24 +00:00
};
this.getDevices = function() {
var devices = [];
var result = SHELL.exec([this.binPath, "devices"]);
splitLn(result).forEach(function(line) {
var row = line.split(/\s+/);
if(row.length == 2) {
devices.push({
id: row[0],
hostname: this.getHostname(row[0]),
type: row[1]
});
}
});
2022-03-01 20:58:39 +00:00
return devices;
2022-03-01 18:15:24 +00:00
};
this.getHostname = function(id) {
2022-03-02 14:51:07 +00:00
var result = this.getProperty(id, "net.hostname");
if (typeof result === "string") {
return result.trim();
} else {
return "";
}
2022-03-01 18:17:42 +00:00
};
this.getProperty = function(id, name) {
2022-03-02 09:05:34 +00:00
return this.sendShell(id, ["getprop", name]);
2022-03-01 18:15:24 +00:00
};
this.disableService = function(id, name) {
2022-03-02 09:05:34 +00:00
return this.sendShell(id, ["svc", name, "disable"]);
2022-03-01 18:15:24 +00:00
};
this.enableService = function(id, name) {
2022-03-02 09:05:34 +00:00
return this.sendShell(id, ["svc", name, "enable"]);
};
this.sendShell = function(id, args) {
try {
return SHELL.exec([this.binPath, "-s", id, "shell"].concat(args));
} catch (e) {
return "";
2022-03-02 09:06:11 +00:00
}
2022-03-01 18:15:24 +00:00
};
2022-03-02 15:17:00 +00:00
// download a file from target device
this.pull = function(id, path) {
return SHELL.exec([this.binPath, "-s", id, "pull", path, "data\\"]);
};
// upload a file to target device
2022-03-02 15:17:32 +00:00
this.push = function(id, filename, path) {
return SHELL.exec([this.binPath, "-s", id, "push", "data\\" + filename, path]);
2022-03-02 15:17:00 +00:00
};
2022-03-02 15:23:26 +00:00
// install APK file
2022-03-02 15:25:11 +00:00
this.install = function(id, filename) {
2022-03-02 15:23:26 +00:00
return SHELL.exec([this.binPath, "-s", id, "install", "data\\" + filename]);
};
2022-03-02 15:25:11 +00:00
// Uninstall the App
this.uninstall = function(id, appname) {
return SHELL.exec([this.binPath, "-s", id, "uninstall", appname]);
};
2022-03-02 15:23:26 +00:00
// reboot device
this.reboot = function(id) {
return SHELL.exec([this.binPath, "-s", id, "reboot"]);
};
2022-03-01 18:15:24 +00:00
}
exports.create = function() {
return new ADBObject();
};
2022-03-01 18:55:24 +00:00
2022-03-02 09:05:34 +00:00
exports.VERSIONINFO = "Android Debug Bridge Interface (adb.js) version 0.2";
2022-03-01 18:55:24 +00:00
exports.global = global;
exports.require = global.require;