Ethernet over USB + ADB | Dynamically switch between Host and Peripheral mode in OTG

634 Views Asked by At

Problem:

I own an android device in a remote location that I use to run various tests and operations. I need to be able to send ADB commands while also connect the phone to a wired internet connection. For various reasons, I cannot connect the device to WiFi, nor can I root the device. Due to USB Enumeration structure, if I connect a USB hub to the phone with an Ethernet port and a USB connection to a PC, the options are either Phone in Host + Internet but no ADB, or phone in Peripheral mode with ADB but no ethernet connection.

What I have tried:

  • Setting adb tcpip 5555 to enable ADB over a network connection (TCP mode). This means connecting the phone through USB, setting adb tcpip 5555 to enable ADB over network, and then disconnecting the USB and replacing the connection with the Ethernet dongle in host mode. This works fine until the phone is rebooted and then manual intervention is needed.
  • Various USB dongle/hub set ups.
  • LADB workaround where the phone connects to itself through wireless ADB, then sets the adbd to TCP mode, etc. For this, I need a WiFi network so it does not meet the requirements. It's also very flaky.
  • Internet Over USB (gnirehtet, etc). This solution does not work because it requires VPN, and also requires a USB connection to a machine, while I prefer just power and the ethernet connection.

What I want to do: I'm wondering if I can purchase a Raspberry Pi or some other microcontroller, and implement a solution using Session Request Protocol (SRP) and/or Host negotiation Protocol (HNP) described in On-The-Go (OTG) as follows:

  • Connect Android device using USB-C (as OTG cable) to companion device.
  • Connect companion device to internet through ethernet port
  • Setup states
  1. Software in Android/external code detects when adbd is in USB mode, and renegotiates the connection to Android being in device mode. Companion uses USB connection to send adb tcpip 5555.
  2. Once adbd is set to TCP mode, software (using getprop) checks that phone accepts ADB over network, and renegotiates the usb connection such that the Android is now Host, and accepts the companion device as an Ethernet dongle, and internet flows to the device.

This way, the network connection to the device (including sending ADB commands) remains stable even after device reset. There are also power considerations, but they are secondary.

1

There are 1 best solutions below

0
RE_Specto On

sounds like quite of a challenge.. afaik the PI zero is able to act as both usb host as well as usb Ethernet Gadget. you may be able to leverage that to send an 'adb tcpip 5555' from the PI, then switch modes, and share the PI's connection to android. however if you cannot use the wifi on the PI as well, you'll need to provide it with ethernet via GPIO, not shure how its done on the zero. as well the ability of the PI to switch modes on the fly without reconnecting the usb cable needs to be verified.

another way would be using a rooted android, and set the local equivalent of the adb command (at each boot?).

a side note: i have tried your first method of issuing an adb command via usb to my pc, and then switching cables to an otg ethernet adapter, and besides not surviving a reboot, the performance of scrcpy which is my use case for remote debug was less than acceptable.

I have ended up using the usb, and the combination of gnirehtet and scrcpy over usb with some great performance, and no electromagnetic radiation emitted from my phone besides the basic cell service, while the rest of its comms are handled via usb, with the only caveat of having to accept vpn connection once, which is done via scrcpy as well without touching the phone.