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

前端 未结 5 1306
耶瑟儿~
耶瑟儿~ 2021-02-01 10:11

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. Typ

5条回答
  •  青春惊慌失措
    2021-02-01 10:49

    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.

提交回复
热议问题