0

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());
 }
 });
asked Jan 18, 2016 at 20:51

2 Answers 2

0

What is entrada? Replace entrada with enter in your arduino code and try again. Also try this run() code instead:

public void run()
{
 byte[] espera = new byte[1024];
 int esperaalmacen;
 while (true) {
 try {
 esperaalmacen=entradadatos.read(espera);
 mhandler.obtainMessage(MENSAJELEYENDO,esperaalmacen,-1,espera)
 .sendToTarget();
 }catch (IOException e){break;}
 }
}
answered Jan 19, 2016 at 0:27
1
  • Entrada is from my class Connection(other class), that i did use when i would want connect my device with arduino Commented Jan 23, 2016 at 19:25
0

First try

if(conect.available()>0) { enter=conect.read(); if(1) { digitalWrite(8,HIGH); conect.print("1"); } } }

If that works, think about what entreda is and that exactly you're trying to do there.

answered Jan 18, 2016 at 22:56
1
  • Sorry, your idea is the same that i have in my codec :( Commented Jan 18, 2016 at 23:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.