I send a date from my android to arduino without problem, but also i want send date from arduino to android later of received the date from android.
I have time trying it, but i can not yet, help me. My codec of 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");
}
}
}
And in the side from android this is my handler for drive the the date 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;
}
}
};
And on my class for read and write i have this
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){}
}
}
When i push button, send a date to arduino, here my codec of button(i send 1 for bluetooth)
Enviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String salida="1";
msjre.escribe(salida.getBytes());
}
});