mobile-dev-inc / dadb

A Kotlin/Java library to connect directly to an Android device without an adb binary or an ADB server
Apache License 2.0
1.02k stars 73 forks source link

Use Dadb to connect two Android devices (phone/watch) #13

Closed malbry closed 2 years ago

malbry commented 2 years ago

Hi, I'm interested in using this library to connect an Android phone to an Android (WearOS) watch, with the Dadb-enabled app running on the phone. ADB over wifi would be enabled on the watch, and I would know the ip address. What I'd hope for is that the phone app makes an adb connection request to the watch's ip address. This would generate a prompt on the watch that the user can accept or reject. If the request is accepted, then the connection is established. This is broadly how I've seen other ADB libraries work.

But I can't figure out how to accomplish that using Dadb. The only example given is connecting to an emulator. The real power of this library would be to connect two actual Android devices over wifi. Please say if this is possible with the current version of Dadb and, if so, give some brief example code.

Thanks.

dmitry-zaitsev commented 2 years ago

@malbry I believe this should work:

Dadb.create("hostname of the watch", 5555)

That assumes that you know the IP of the watch

malbry commented 2 years ago

Unfortunately not. The watch has ADB debugging enabled and has a fixed ip address. dadb.discover() won't find it. dadb.create() doesn't prompt a request on the watch to accept an ADB connection, and subsequent attempts to use dadb.install() just give an error message.

Leland-Takamine commented 2 years ago

dadb.create() doesn't prompt a request on the watch to accept an ADB connection

@malbry Which version of the library are you using? Version 0.0.8 updated dadb.create() to use your default ~/.android/adbkey which should trigger the adb prompt on the device.

malbry commented 2 years ago

Thank you for the replies. I am using library version 0.0.9. Perhaps I'm missing something, but ~/.android/adbkey is assuming a desktop system, is that correct? I'm trying to connect a phone to a watch over ADB. The Java-based library that I'm currently using (https://github.com/cgutman/AdbLib) generates the key automatically but it only implements shell whereas your solution also implements push/pull/install/uninstall.

If I need to generate the key myself to use your library then can you give me any pointers on how to do that?

Edited to add: if you have any code that establishes an ADB connection over wifi between two Android phones (where one is acting as the host and the other as the receiver) then that would be excellent. I can then adapt as necessary for my use case which is phone to watch.

Many thanks!

malbry commented 2 years ago

I'm guessing that it's not possible to do what I'm asking (phone to phone/watch connection over wifi) otherwise someone would have jumped in to tell me how to do it?

amanjeetsingh150 commented 2 years ago

Hey @malbry we are investigating this, will be back with the answer soon 😄

amanjeetsingh150 commented 2 years ago

Hey, @malbry thank you for waiting, I was going through the discussion.

~/.android/adbkey is assuming a desktop system, is that correct? That is correct. You can see this here.

If you want to generate the keypair yourself, this is the same file you need to change. The library that you referred to I saw is generating public and private key here.

However, can you also elaborate on your use case I'm not sure if I get it right but why would the default ADB keypair would not work in your use case? We can collaborate on seeing if we can accommodate this particular use case 😄

malbry commented 2 years ago

I'd love to collaborate, thank you. To be more specific, my use case is as follows. I want to be able to install apk files from my Android phone to my WearOS watch over ADB. So I will firstly enable ADB debugging over wifi on the watch, then connect my phone (as host) to the watch (as receiver) using DADB. Then I want to issue an adb install command from my phone to install the apk file from the phone to the watch. If you don't have a WearOS watch then testing on two Android phones would work (ie. install apk from one phone to the other).

When I tried using the Dadb.create("ip address of watch", 5555) command suggested above, I couldn't get any prompt on the watch to accept the connection from the phone. Maybe I needed to create a key, because it didn't seem to do that for me. If easier, my email address is malcolm (at) freepoc.org. Thank you!

malbry commented 2 years ago

Update 19 April: I have managed to get it working for my use case, thanks. Unfortunately you don't implement methods to generate ADB keypairs in Dadb (this would be a really useful enhancement). But I was able to generate the keypairs using code from the other library AdbLib and, with some tweaks to the the file format, get Dadb to use them. By the way, your documentation says you can use custom keypairs using AdbKeyPair.create(). This is a mistake, that method does not exist. The correct usage is AdbKeyPair.read().

amanjeetsingh150 commented 2 years ago

Ohh that's great @malbry, glad to hear it worked for you. I see it is definitely important when any android device is acting as adb host and is not a system right. Currently, we are assuming a system instance (normal file system). I will chat with the team on this feature request and will keep you updated.

Regarding,

By the way, your documentation says you can use custom keypairs using AdbKeyPair.create(). This is a mistake, that method does not exist. The correct usage is AdbKeyPair.read().

I'll check this and create fix. Thanks for helping again!

Leland-Takamine commented 2 years ago

@malbry I just release v0.0.11 which introduces a AdbKey.generate(privateKeyFile, publicKeyFile) method. You can use this to generate the keys on your device. I'll close this issue for now, but please re-open if this method doesn't work for you. Thanks again for surfacing this use case!

(Note: v0.0.11 may take a little while before it propagates to Maven Central.)