I am working on a communication between android and arduino board.

The android make a TCP connection with the board and successfully send some string to the board.

The problem is , there is a int in my arduino code (eg. int distance) and I would like to pass that back to my android device

I have handle 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
                        mMessageListener.messageReceived(serverMessage);
                    }
                    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 a integer variable (without concern what is the source device)?

What I should do in ardino for making that response? Thanks for help.

share|improve this question
    
is serial.println(distance); correct? thanks – user782104 Mar 4 '15 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. – Chris Stratton Mar 5 '15 at 15:16
up vote 1 down vote accepted

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.print(distanceValue);
  client.close(); //close connection
}

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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