Communication between android and arduino

I am send a date from android to arduino(module hc-05) without problem, now i want when a date arrive from android to arduino also send a date from arduino to android for mean bluetooth.

My module is a zs-040.

//This is my codec from arduino
char enter;
#include <SoftwareSerial.h>
SoftwareSerial conect(10,11);

void setup()
{
pinMode(8,OUTPUT);
conect.begin(9600);
digitalWrite(8,LOW);
}

void loop()
{
if(conect.available()>0)
{
enter=conect.read();
if(entrada==‘1’)
{
digitalWrite(8,HIGH);
conect.print(“1”);
}
}
}

This is my part on android where i drive all data that arrive

Handler mhandler=new Handler(){
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
switch (msg.what)
{
case CONECTADO:
//ManejarDatos msjre = new ManejarDatos((BluetoothSocket)msg.obj);
msjre = new ManejarDatos((BluetoothSocket)msg.obj);
break;
case MENSAJELEYENDO:
byte readBuf = (byte)msg.obj;
String string = new String(readBuf,0,msg.arg1);
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG).show();
break;
}
}
};

This is my class special for read and write data of android.

private class ManejarDatos extends Thread
{
private final OutputStream salidadatos;
private final BluetoothSocket sockk;
private final InputStream entradadatos;
public ManejarDatos(BluetoothSocket socke1)
{
sockk=socke1;
OutputStream temporalsalida=null;
InputStream temporalentrada=null;
try
{
temporalentrada=socke1.getInputStream();
temporalsalida=socke1.getOutputStream();
}catch (IOException e){}
salidadatos=temporalsalida;
entradadatos=temporalentrada;
}
public void run()
{
byte espera;
int esperaalmacen;
while (true) {
try {
espera=new byte [1024];
esperaalmacen=entradadatos.read(espera);
mhandler.obtainMessage(MENSAJELEYENDO,esperaalmacen,-1,espera)
.sendToTarget();
}catch (IOException e){break;}
}
}
public void escribe(byte bytes)
{
try {
salidadatos.write(bytes);
//handler.obtainMessage(MENSAJE_SALIDA,-1,-1,bytes).sendToTarget();
}catch (IOException e){
//Log.e(TAG, “…Error al enviar dato” + e.getMessage() + “…”);
}
}
public void cancel()
{
try{
sockk.close();
}catch (IOException e){}
}

}

http://www.geothread.net/arduino-making-a-simple-bluetooth-data-logger/

Use the above link to verify your Arduino to Android Bluetooth communication - use in setup()..

Than use the "wait for response " function in loop() to receive incoming data FROM Android ( serial data stream) and than modify the functions you used in setup to send whatever back to Android.

Actually I would put the functions used in setup into another function and just send it back as a response to the received code from Android.

in pseudocode
in setup setup Bluetooth communication FROM Arduino to Android and verify responses from Android side.

in loop
wait for any data from Android Bluetooth
when detected
send SAME code as in setup and verify the response again

You will need some means to stop this test loop - send some kinda of terminating character from Android .
And when yu have this done - work on real processing of data between both devices.

Keep in mind that Bluetooth is just wireless modem and on Arduino side treat it as serial communication.

Please read #7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html

I've used code like below to communicate between two arduinos.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino tx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test
//A diode between the slave tx and master rx is suggested 
//for isolation, with diode band attached to slave tx side. 
//

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}