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.