8

How can I can send a string (ex: "hi") to my PC from my Android phone?

hichris123
  • 10,145
  • 15
  • 56
  • 70
FlashCreated
  • 89
  • 1
  • 1
  • 3
  • FYI netcat (often installed as nc) is a great command-line tool for debugging such projects as it can substitute for either end. And it's available for just about everything, including android phones. – Chris Stratton Dec 28 '10 at 17:47
  • Checkout [this question](http://stackoverflow.com/q/8443245/393021) – Igbanam Mar 29 '13 at 22:34

2 Answers2

7

Before doing this in Android, I recommend you read the basics of Java networking:

Try to get access to a good book. Once you know how to do it in Java, implement a test client in Android & a test server in your PC.

Sebastian Roth
  • 11,344
  • 14
  • 61
  • 110
  • Thanks for the resonce, i've found some code on this page that i think should do it. I need to be able to run the client programe on my htc (android 2.1). Could someone please tell me how??? – FlashCreated Dec 28 '10 at 17:12
7

I'm actually implementing a socket solution between Android and PC right now! Once you understand the basics behind sockets (by reading the links that Sebastian suggested), you can use the following links as code guides to make your instruments talk:

I'm using the client code from the Android socket example link above, and the server example from the Unix socket example on Beej's website (I've been informed that I'm a new user to stackoverflow, so I'm going to link that as a comment to this answer). The Unix socket server is set up to receive a connection and a packet from the client, then send an answer back and close the socket. I modified this so that the server is just a loop that continuously asks for input from the user at the console. It's not a pretty solution (i.e. no user interface on the PC side), but it will at least give you the basics. You can make it pretty later.

moscro
  • 486
  • 5
  • 13
  • Here's the link to Beej's website: http://beej.us/guide/bgipc/output/html/multipage/unixsock.html – moscro Dec 28 '10 at 17:31
  • Thanks a lot, that looks great but but would you mind telling me how to implement that client code in an application, I've put it into the main ".java" file of my progect ad i'm getting errors from everywhere (programming in eclipse)... thanks in advance! – FlashCreated Dec 28 '10 at 17:50
  • Okay, at this point it sounds like you're brand new at writing Android apps. I would study this documentation: http://developer.android.com/reference/android/app/Activity.html : http://developer.android.com/resources/articles/painless-threading.html and buy a book, Sam's Teach Yourself Android Programming in 24 hours was worth the money. These key concepts need to be understood first before you can implement a network communication application. – moscro Dec 28 '10 at 22:55
  • Here is a complete app for accomplishing Bluetooth communication between devices, which should be similar in structure to the way you should set up your application once you understand Activities: http://developer.android.com/resources/samples/BluetoothChat/index.html – moscro Dec 28 '10 at 23:06
  • 2
    Ok thanks everyone! I'll do my homework and try again after... – FlashCreated Dec 28 '10 at 23:33