Skip to content

February 15, 2010

10

Communicating over the USB cable

For the eTegrity project I’m working on for my senior design, I needed the smartphone and the computer to communicate over a USB connection. This turned out to be an unusually difficult thing to figure out, but really simple otherwise.

Now, an important thing to note here is that it does require software to be installed on the host computer, specifically the adb tool from the Android SDK and of course the driver (this depends on your OS, see details here)

That being said, all you need to do is to activate port forwarding using adb. What this does is it maps a port on your local computer to a port on the smartphone. For example:

adb forward tcp:1234 tcp:1234

If it returns no message, it executed correctly, otherwise it could be that the port you specified cannot be binded on one of the two machines or some other issue (it will tell you). The link above is great for figuring out how to use adb, so just check it out.

Then, you can go ahead and implement regular sockets on both machines. For some reason, at least in my case the server socket had to be on the smartphone, all attempts to have it be on the computer failed. So put the server socket on the phone, client socket(s) on the computer and you’re all set.

Make sure when you implement the socket on the phone that your application specifies the “INTERNET” permission in its manifest. I know, you’re only trying to do a local network connection, but you need that to be able to open sockets.

I’ll try to post some sample code to go together with this explanation soon.

Notes:
- I am using a Windows XP SP 3 box and an HTC Hero phone (running Android 1.5)
- I believe that in Linux there is a simpler way of doing this, but I haven’t tested at all
- You do have to run the adb port forwarding command again after reboot
- Since 2.0 they introduced Bluetooth so you can just do the socket thing over Bluetooth directly without the adb stuff. Then again, not many devices out there running 2.0 right now as far as I know.

Later Edit: Some sample code here.

10 Comments Post a comment
  1. Oct 6 2010

    waiting your sample code…

  2. Oct 11 2010

    Yilmaz, I will try to get code posted within a week, but I can’t promise anything. I’m really sorry about not doing it yet :(

  3. vvis
    Feb 17 2012

    Can we put the client socket on the phone, server socket on the computer ? Can it be done? Thx!

  4. Feb 18 2012

    vvis, please read the post I linked to above with the sample code and also the comments for that post ( http://www.anothem.net/archives/2010/10/15/android-usb-connection-to-pc/ ).
    tl;dr: No, it doesn’t appear to be possible.
    Thanks.

  5. lydia
    Mar 25 2012

    I like the code you posted and am using android 2.2 interfacing via a usb interface to an external hardware and was wondering if you would have any links on the code I need to allow USB debugging?

    Thank you!

  6. Mar 25 2012

    lydia, I’ve only ever worked with an USB connection between Android and a PC, never with an external piece of hardware, so I’m afraid I don’t have anything for you.

    I was also under the impression that it wasn’t even possible to connect an Android device with another piece of hardware over USB, I’d be really interested in seeing a working project that does that. Please let me know if you find out how to do what you want.

  7. lydia
    Mar 25 2012

    Thanx a lot and will let you know. Enough projects have been done on that especially those that deal with Android as host mode to external hardware though its mainly from 3.1 version and above so I need an application that enables me to include my own library and still hope it is possible.

  8. Shubham
    Mar 27 2014

    is there any difference between TCP\IP Socket Connection and ADB Socket Connection ???

Trackbacks & Pingbacks

  1. With our powers combined… sample code! | Seconds & pixels
  2. Speed of forwarding ADB ports to send data from Android device to PC | Technology & Programming

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

Current day month ye@r *