How can I initiate an Android-to-PC HTTP request over USB?

18,585

Solution 1

It is not actually the communication which is one way, but the ability initiate connections.

To do this with standard android functionality, you will conceptually have to tunnel the device-initiated connections through an existing PC-initiated connection. For example, using off the shelf tools:

  1. Set up an ADB port forward from PC to device
  2. Run an SSH server on the android device on an unprivileged port number corresponding to your adb forward
  3. Connect to the phone from an SSH client on the PC through the ADB forward, and set up an SSH forward of a port on the phone back to a port on the PC
  4. Run a server on the PC listening at the destination port of the SSH forward.
  5. Have your code on the phone contact the server on the PC via that SSH port forward

You don't actually have to use SSH, it's just that this packages up the pieces ready to use.

As an alternative idea, some Android devices which support USB tethering to provide mobile network service to a PC implicitly end up creating a bi-directional IP network over the usb cable, which you can use for other purposes. But be very careful that you don't accidentally route all the PC's network traffic through the phone.

Solution 2

If the Android phone supports USB tethering:

  1. Connect the phone through USB cable
  2. Enable USB tethering
  3. A Local Area Network connection would be created in the IP Range 192.168.142.X [The IP for this LAN can be made static as well]

Now a web server running on the PC can be sent HTTP requests with this setup.

Solution 3

In USB it is always the host (the PC in your case) which initiates the communication. So you'll need to use tricks to appear that the Android device sends a request. The PC might periodically poll Android (say, every 100 ms) if it has a HTTP request pending and collect it accordingly, similar to what jackbot did. I guess that's what Android to USB Port Forwarding did.

Share:
18,585
Tom Wright
Author by

Tom Wright

Father, amateur zoologist, music-lover, and software developer. Aspiring blogger, occasional Tweeter, and diligent Scrobbler Head of software development at Global Pricing Innovations.

Updated on September 14, 2022

Comments

  • Tom Wright
    Tom Wright over 1 year

    This has been asked before, but did not receive a satisfactory answer.

    From my Android application, I need to send an HTTP request to a PC attached via USB. Typically, communication using ADB is one-way, from PC to Android.

    As mentioned in the answers to the aforementioned duplicate, this has been done before: Android Usb Port Forwarding. If I really needed to, I could just download this app and botch it, but I'd much rather implement this within my own app.

    Any ideas?

  • Tom Wright
    Tom Wright almost 12 years
    This strikes me as inelegant, but at least we know it'll work I guess.
  • Chris Stratton
    Chris Stratton almost 12 years
    @Tom Wright - unless you create/repackage the pieces yourself, it's more about setting up existing tools than coding.
  • Chris Stratton
    Chris Stratton almost 12 years
    There seems to be a little confusion here between two very distinct layers. In terms of low-level USB (which is not really available to change on secured phones), such polling is already going on as part of the USB stack. In terms of a forwarded TCP connection from PC to device, polling is not needed because the connection once established is bidirectional. You could use that bidirectional connection to request that the PC establish yet another connection, or (more likely) you could tunnel something else back through it.
  • Tom Wright
    Tom Wright almost 12 years
    OK, well I've awarded the bounty. I'll give it a try over the next few days and mark it accepted if it works.