在设备本身上运行adb,即就像发出命令的PC一样 [英] Run adb on the device itself, i.e. as if it were the PC issuing the commands

查看:116
本文介绍了在设备本身上运行adb,即就像发出命令的PC一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是编写一个可以在手机上运行的应用程序,让用户从APK列表中进行选择,然后将所选的应用程序安装到同一网络上的Android Things设备上。

My goal is to write an app which runs on a handset and lets the user choose from a list of APK's, then installs the one selected to an Android Things device on the same network.

我们实际上可以忘记Android Things,因为相同的代码可以在两部手机之间工作,只是没有意义,因为目标可以通过其他方式(如附件,BT等)接收APK。除了最近宣布的Android Things Console(对于常规的本地开发而言已过时)之外,AT设备仅为此使用ADB。因此,我希望从手机本身复制PC进行安装的顺序,即 adb connect,adb install ...等。我们可以假设所有涉及到的设备都是植根的。

We can actually forget Android Things because the same code would work between two handsets, it's just that there it would be pointless because the target can just receive the APK in many other ways such as an attachment, BT etc. AT devices only have ADB for this, apart from the recently announced Android Things Console which is overkill for regular local development. I'm therefore looking to replicate the sequence a PC would go through to install it, i.e. "adb connect, adb install ..." etc but from the handset itself. We can assume all devices involved are rooted.

在我看来,这意味着我的应用必须以过程的形式发出这些命令,但是我很难获取它可以正常工作。当我发出 adb帮助时,我会获得帮助消息,而当我发出 adb重新启动时,设备将重新启动,因此我认为我的做法正确。当我尝试除此以外的任何方法都无济于事时, adb shell ping -c 1 192.168.62.40示例失败,但从PC上的ADB可以。同样,它很好奇 adb版本失败,在终端上也可以。

It seems to me this means my app has to issue these commands as a process, but I'm having a hard time getting it fully working. When I issue "adb help" I get back the help message, and when I issue "adb reboot" the device reboots, so I think I'm on the right lines. It's when I try anything apart from those I get nothing back - the example of "adb shell ping -c 1 192.168.62.40" fails, but is OK from ADB on the PC. Also, it's very curious "adb version" fails which again is OK from the terminal.

起初,我认为手机可能只安装了 adb lite刚好足以让它们作为adb目标工作,但使用PC上的外壳显示情况并非如此。

At first I thought the handsets might only have an "adb lite" installed which has just enough to get them to work as an adb target, but using a shell from a PC showed that's not the case.

这是我正在尝试的代码:

Here's the code I'm trying:

Process process = Runtime.getRuntime().exec("su adb help");
//Process process = Runtime.getRuntime().exec("su adb reboot");
//Process process = Runtime.getRuntime().exec("su adb version");   
//Process process = Runtime.getRuntime().exec("su adb shell ping -c 1 192.168.62.40");
process.waitFor();

Log.d("PROCESS", "Status: "+process.exitValue());
BufferedReader bufferedReader = new BufferedReader(
        new InputStreamReader(process.getInputStream()));

StringBuilder everything = new StringBuilder();
String line;
while( (line = bufferedReader.readLine()) != null) {
    everything.append(line);
}

Log.d("PROCESS", "Process output: "+everything.toString());

Toast.makeText(MainActivity.this, everything.toString(), Toast.LENGTH_SHORT).show();


推荐答案

问题是 adb 客户端(用于与开发计算机上的设备进行通讯的ADB 服务器进行通信)未打包在Android设备。但是, adbd 守护程序(用于在 adb 客户端和设备之间进行通信)可以在以下位置找到:一个Android系统。

The problem is that the adb client (which you use to communicate to the ADB server which communicates to and from a device on a development machine) is not packaged on an Android device. The adbd daemon, however, (used to communicate between the adb client and the device) can and will be found on an Android system.

有一些选项可帮助在Android设备上使用 adb 客户端:

There are a few options to help use the adb client on an Android device:


  • 尝试通过adb 客户端github.com/android/platform_system_core/tree/master/adb rel = nofollow noreferrer> adb GitHub上的资源。

  • 尝试进行临时的 adb 客户端。由于 adb 客户端和 adbd 守护程序通过USB或TCP进行通信,因此您可以尝试模拟通信协议以打开读取/在设备上写入流。有关客户端-守护程序通信协议的更多信息,请此处。我正在使用的该库可能会为您提供帮助: eviltak / adb-nmap

  • Try and build the adb client for an Android device from the adb sources on GitHub.
  • Try and make a makeshift adb "client". Since the adb client and adbd daemon communicate via USB or TCP, you could try emulating the communication protocols to open read/write streams on the device. More on the client-daemon communication protocols here. This library that I am working on might help you: eviltak/adb-nmap

快速而肮脏的选择是从源代码构建 adb 客户端并将其推送到设备。也就是说, if 您可以将其构建在Android设备上。

The quick and dirty option is to build the adb client from sources and push to the device. That is, if you can get it to build on an Android device.

第二个选项可能是最耗时的,但可以最干净的选择。但是,您将必须模拟 adb 身份验证系统,正确处理流等,这可能很麻烦。源代码将为您提供帮助。

The second option is probably the most time consuming, but can be the most "clean" choice. You will, however, have to emulate the adb authentication system, properly handle streams and so on, which can be cumbersome. The source will help you.

无论哪种情况,GitHub adb 源代码目录都应包含您需要的所有内容。漫漫长路。

In either case, the GitHub adb source directory should have everything you need for the long road ahead.

这篇关于在设备本身上运行adb,即就像发出命令的PC一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆