似乎无法将client.shell()命令的输出保存到一个变量中。

问题描述 投票:0回答:3

我正在使用node-webkit和ADBkit试图从android build.prop中读取一行,并根据该行的内容做一些事情。

完整的脚本在 http:/pastebin.com7N7t1akk

它的要点是这样的。

var model = client.shell(devices, "su -c 'grep ro.product.model /system/build.prop'" );  
alert(model)

我想读 ro.product.model 变量中,将build.prop中的 model 作为一个测试,我只是简单地尝试创建一个 alert 显示这个shell命令的返回值,在我的例子中就是 ro.product.model=KFSOWI 但每当我在连接的设备上运行这个脚本时,都会出现以下情况 alert 返回 object Object

编辑** 我刚刚意识到 client.getProperties(serial[, callback]) 可能会更好,但不太了解这些功能(特别是回调)。

我是一个非常新的Javascript领域和家庭有人可以提供一些见解。

javascript node.js adb node-webkit node-modules
3个回答
0
投票

JavaScript是一种异步编程语言,它建立在回调之上。每个函数都应该有回调与数据传递给它,如果你会看上 文件,你有 client.shell(serial, command[, callback]) 所以,执行中的数据 client.shell() 将传给 callback. 你应该分配一些函数来处理回调,对于你的案例将是这样的。

client.shell(devices, "su -c 'grep ro.product.model /system/build.prop'", function(data) {
    console.log(data);
});

P.S. 没有 alert 在nodejs中


0
投票

根据文档,你可以在client.shell()的回调的第2个参数中捕获输出。

client.shell(devices, "su -c 'grep ro.product.model /system/build.prop'", function(err, output) {
  if (err) {
    console.log(err);
  }
  console.log(output);
});

0
投票

使用 async await 以获得更清晰的代码。

const data = await client.shell(devices, "su -c 'grep ro.product.model /system/build.prop'" );

console.log(data); // => "Samsung.TM395"

当然,只有当这段代码在一个 async 功能。

流式数据。

对于使用adbkit的流式数据,你需要做更多的事情来读取整个流,然后输出结果,就像这样。

const stream = await adbClient.shell( config.udid, "ime list -s" ) // adb command to list possible input devices (e.g. keyboards, etc.).
const result = await adb.util.readAll( stream );
console.log( result.toString() ); // => com.sec.android.inputmethod/.SamsungKeypad
© www.soinside.com 2019 - 2024. All rights reserved.