I am working on a communication between Android and Arduino board.

The Android makes a TCP connection with the board and successfully sends some strings to the board.

The problem is, there is an int in my Arduino code (e.g. int distance) and I would like to pass that back to my Android device.

I have handled the Android side already, but I do not know how to return a message from Arduino.

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                //in this while the client listens for the messages sent by the server
                while (mRun) {
                    serverMessage = in.readLine();

                    if (serverMessage != null && mMessageListener != null) {
                        //call the method messageReceived from MyActivity class
                    serverMessage = null;


This is the Android side code but I think that it is not a specific Android problem, there should be some way for Arduino to make a response of an integer variable (without concern what is the source device)?

What I should do in Arduino for making that response?

  • is serial.println(distance); correct? thanks
    – user782104
    Mar 4, 2015 at 18:14
  • 1
    The binary representation of integers varies between Arduino (16 bit) and Android (32) bit, and while both an Arduino and most native-level Android implementations are little-endian, Android java code is big-endian. So transferring your values in a string format may ease compatibility, as well as making it easy to debug the system by substituting a terminal emulator. Mar 5, 2015 at 15:16

1 Answer 1


I'm assuming that you are using an Ethernet shield, as nothing about this is stated. But solution is really similar between many network solution.

Once a TCP connection has been established, it can be used like you would use the Serial;

you'll probably end up having something similar to:

EthernetClient client = server.available(); //accept a new connection
if (client) { //if valid connection
  client.close(); //close connection

look at the official API reference for a more detailed list of available command.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.