ali mohammadi
ali mohammadi

Reputation: 43

delay in receiving data from Android to Arduino

I'm connecting my android device to arduino via USB and I receive data immediately from arduino by using bulkTransfer, but when I'm going to send acknowledge signal back to arduino using the same command, it receives that some seconds later.

My arduino model is DUE, and the arduino side code is:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(49, OUTPUT);
}
void loop() {
  String a;

  int sensorValue[64];
  for (int i = 0; i <= 63; i++)
  {
    sensorValue[i] = analogRead(A0) / 4;
    a = a + char(sensorValue[i]);
  }
  char b;
  while (1) {
    Serial.print(a);
    Serial.flush();
    delay(2);
    b = Serial.read();
    if (b == '1')
    {
      break;
    }
  }
  digitalWrite(49, HIGH);
  delay(2000);
  digitalWrite(49, LOW);
}

My android side code is:

public void onClick(View v) {
            synchronized (this) {
                if (usbDeviceConnection != null) {

                    Thread myThread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                int chunkCounter = 0;
                                String strTemp = null;
                                while (chunkCounter < 1) {

                                    byte[] message = new byte[64];

                                    final int result = usbDeviceConnection.bulkTransfer(endpointIN, message, 64, 10);

                                    if (result == 64) {
                                        for (int intTemp = 0; intTemp < 64; intTemp++) {
                                            strTemp += String.valueOf((char) message[intTemp]);

                                        }

                                        byte[] ack = new byte[1];
                                        ack[0] = 1; //1 means I've got this chunk
                                        synchronized(this) {
                                            int resultAck = 0;
                                            while (resultAck <= 0) {
                                                resultAck = usbDeviceConnection.bulkTransfer(endpointOUT, ack, 1, 10);
                                            }
                                        }
                                        chunkCounter++;
                                        if (chunkCounter == 1) {
                                            final String strTempFinal = strTemp;
                                            tempflag = true;
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    txtGetData.setText(strTempFinal);
                                                }
                                            });
                                        }
                                    }
                                }
                            } catch (final Exception e){
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }
                    });
                  myThread.start();


                }

Any help would be appreciated. thanks in advance.

Upvotes: 0

Views: 164

Answers (0)

Related Questions