Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is Adb Server possiblely convert to Java? #101

Closed
Trumeet opened this issue Sep 11, 2018 · 5 comments
Closed

Is Adb Server possiblely convert to Java? #101

Trumeet opened this issue Sep 11, 2018 · 5 comments

Comments

@Trumeet
Copy link

Trumeet commented Sep 11, 2018

I saw that the project is the client of Adb, it creates socket connection to Adb Server. Is it possible to make a Adb Server in Java?

@vidstige
Copy link
Owner

It's possible, but unwise. The problem is you work with the OS level USB interface to communicate with the device. I've never heard of such enterprise, but I'm pretty sure it can be done. Normally I would advice a language like C or perhaps C++ for this type of task.

@mirfatif
Copy link

@vidstige AdbLib provides ADB over network functionality of ADB server (though limited) without running a server. Isn't something similar possible in jadb without getting involved in complexities of ADB over USB?

@vidstige
Copy link
Owner

@mirfatif this implementation is already present in jadb. There is an almost complete server implementation used in the automatic tests. However, it still does not help with the original problem of talking directly to a phone only using java - You still need the USB parts. However, if there was a way to run via network, that would be awesome. In adb the adb damon is running on the phone, the adb server and client is running on the controlling computer. So in this case I guess either the adb server would need to run on the phone somehow, or if it is possible to create a adb daemon client.

@mirfatif
Copy link

I'm just a hobbyist developer, so I can talk noobish.

I guess either the adb server would need to run on the phone somehow ...

Yeah that's the point. ADBLib client can connect to ADB daemon (adbd already running in tcpip mode obviously) over network socket without running ADB server on phone. This is what I tested:

String command = "ping -c3 1.1.1.1";

Socket socket;
try {
  socket = new Socket("127.0.0.1", 5555);
} catch (IOException e) {
  e.printStackTrace();
  return;
}

File pvtKey = new File(getFilesDir(), "pvt.key");
File pubKey = new File(getFilesDir(), "pub.key");

AdbBase64 adbBase64 = data -> Base64.encodeToString(data, Base64.NO_WRAP);
AdbCrypto crypto = null;
try {
  crypto = AdbCrypto.loadAdbKeyPair(adbBase64, pvtKey, pubKey);
} catch (InvalidKeySpecException | IOException | NoSuchAlgorithmException ignored) {
}

if (crypto == null) {
  try {
    crypto = AdbCrypto.generateAdbKeyPair(adbBase64);
    crypto.saveAdbKeyPair(pvtKey, pubKey);
  } catch (NoSuchAlgorithmException | IOException e) {
    e.printStackTrace();
    return;
  }
}

AdbConnection adb;
try {
  adb = AdbConnection.create(socket, crypto);
} catch (IOException e) {
  e.printStackTrace();
  return;
}

try {
  adb.connect();
} catch (IOException | InterruptedException e) {
  e.printStackTrace();
  return;
}

AdbStream stream;
try {
  stream = adb.open("shell:" + command);
} catch (IOException | InterruptedException e) {
  e.printStackTrace();
  return;
}

while (!stream.isClosed()) {
  try {
    Log.e("Test", new String(stream.read()));
  } catch (InterruptedException e) {
    e.printStackTrace();
    return;
  } catch (IOException e) {
    Log.e("Test", new String(e.toString());
  }
}

However there are some limitations I observed: tananaev/adblib#4.

So is it possible to achieve the same with jadb without relying on the native adb binary on phone?

@vidstige
Copy link
Owner

It's currently not possible as far as I know, but it should definitely be possible then. Still, there would be nice pure-java adb server. But instead we would connect to a adb server on the phone, or talk directly to the adb deamon. There is possibly another protocol in use there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants