1

I am trying to send/receive data from Android to my Arduino (via a HC-05) and later from Arduino to Android. I have configured my HC-05 as slave, and the UART=9600,0,0.... To send data from Android to Arduino I don´t have problem but to send data Arduino to Android I can not...

This is my sketch:

char data;
int a=1;
int v=1;
#include SoftwareSerial connection(10,11);//RX|TX
void setup() 
{
 pinMode(7,OUTPUT);
 connection.begin(9600);
 digitalWrite(7,LOW);
}

void loop() 
{
 if(connection.available()>0)
 {
  data=connection.read();
   if(data=='1')
   {
    digitalWrite(7,HIGH);
    connection.write(data);
    delay(200);
   }
   //connection.write(data);
  }
 }

MY HANDLER IN THE MAIN:

       mhandler=new Handler()        {
        public void handleMessage(android.os.Message msg)            {
            switch (msg.what)
            {
                case MENSSAGESEND:
                    msg = new DriveData((BluetoothSocket)msg.obj);    //DriveData msg = new DriveData((BluetoothSocket)msg.obj);
                    break;
                case MESSAGEREADING:
                    byte[] bufferget = (byte[])msg.obj;
                    String men = new String(bufferget,0,msg.arg1);
                    sb.append(men);
                    int endline=sb.indexOf("\n");
                    if(endline>0)
                    {
                        String print=sb.substring(0,endline);
                        sb.delete(0, sb.length());
                        Toast.makeText(getApplicationContext(),"menssage"+" "+print,Toast.LENGTH_LONG).show();
                        txtData.setText(print);
                    }
                    break;

            }
        };
    };

MY METHOD FOR RECEIVER AND SEND DATA

  public void run()
    {
        byte[] wait=new byte[1024];
        int bytesReceived;
        while (true) {
            try {
                 bytesReceived=entrydata.read(wait);//entrydata is my inputstream
                mhandler.obtainMessage(MESSAGEREADING,bytesReceived,-1,wait).sendToTarget();
            }catch (IOException e){
                break;
            }
        }
    }
10
  • 1
    sorry but.. what is the line #include SoftwareSerial conexion(10,11);//RX|TX? – frarugi87 Mar 2 '16 at 10:07
  • what is HC-05 ? – alcor Mar 2 '16 at 16:58
  • @alcor Bluetooth module. I'd add more details to the question but the Android app is annoying to use. – Avamander Mar 2 '16 at 23:06
  • HC-05 is model from bluetooth for my connection #include SoftwareSerial is the library for use pin´s as RX and TX on arduino UNO, "conexion" is the name from my connection is as variable – Fidel Pineda Santos Mar 3 '16 at 0:15
  • Are you sure it's not a simple hardware problem - like the TX pin being disconnected? You are handling the HC-05 at 3.3 volts right? – Kingsley Mar 3 '16 at 3:13
2

You are missing the line termination you must send '\n' after your data in order to receive the data or use conexion.println(entrada) rather then conexion.write(entrada)

1
  • I already did these, but not work, only i can send data from android to arduino – Fidel Pineda Santos Mar 3 '16 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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