Response is not being receive always in XBEE

Hi
I am trying to know if I have a led On or Off with the XBee.

I am requesting to the router to send a signal to the coordinator to verify this.
My issue is that I'm not always receiving this signal.

This is my arduino code

#include <XBee.h>
#include <SoftwareSerial.h>
int LED = 11;                //Turn this LED on or off, depending on packet rx'd
int debugLED = 13;           //Flash light to indicate rx
int packet;
int pin=12;

XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle 
ZBRxResponse rx64 = ZBRxResponse();

XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x408b2e66);
ZBTxStatusResponse txStatus = ZBTxStatusResponse();

uint8_t payload[]={3};
void setup() {
  // start serial
  xbee.begin(9600);
}

void sendCoordinator(){
  ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
  xbee.send(zbTx);
}

void loop() {    
    xbee.readPacket();
    
    if (xbee.getResponse().isAvailable()) {
       digitalWrite(debugLED, HIGH);
       delay(1000);
       digitalWrite(debugLED, LOW);
      
      if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
        // got a rx packet
        xbee.getResponse().getRx64Response(rx64);
        packet = rx64.getData(0);
       
       if(packet == 0x01){                  //If a '1' is pressed on keyboard side
         if(digitalRead(pin)== LOW){
           payload[0]=10;
         }
         sendCoordinator();
         if (xbee.readPacket(5000)) {
            xbee.getResponse().getZBTxStatusResponse(txStatus);
             if (txStatus.isSuccess() ) {
               digitalWrite(LED, HIGH);              //turn on red LED
             }
         }
         
       }
       else if(packet == 0x00){            //If a '0' was pressed on keyboard side
         digitalWrite(LED, LOW);               //turn off red LED
       }
       else if(packet == 0x02){
         if(digitalRead(pin)== HIGH){
           payload[0]=10;
         }
         else
         {
           payload[0]=20;
         }
         ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
         xbee.send(zbTx);
         
         if (xbee.readPacket(5000)) {
//DEBUG
           digitalWrite(LED, HIGH);
           delay(2000);
           digitalWrite(LED, LOW); 
            if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE) {
               xbee.getResponse().getZBTxStatusResponse(txStatus);
                if (txStatus.isSuccess()) {
//DEBUG
                  digitalWrite(debugLED, HIGH);
                  delay(2000);
                  digitalWrite(debugLED, LOW); 
                }
             }
         }
         else
         {//DEBUG
           digitalWrite(debugLED, HIGH);
           delay(2000);
           digitalWrite(debugLED, LOW);
           delay(2000);
           digitalWrite(debugLED, HIGH);
           delay(2000); 
           
         }
       }
       else{                               //Little debug flashing incase something other than 
         for(int i = 0; i < 10; i++)           //'1' or '0' is rx'd.
         {
           digitalWrite(LED, HIGH);
           delay(1000);
           digitalWrite(LED, LOW);
         }
       }   
    }
    else{
     digitalWrite(LED, HIGH);
     delay(2000);
     digitalWrite(LED, LOW); 
    }
   }
}

This is my java code.

My main issue is in this part ApiId.ZNET_RX_RESPONSE, I'm not always receiving this,

Thanks in advance

    if (payload[0]==2){
      XBeeResponse resp = xbee.getResponse(500);
        System.out.println("Entro a ver el estatus");
       if(resp.getApiId() == ApiId.ZNET_RX_RESPONSE){
public class ArduinoXbee {
    public static void main(String[] args) throws XBeeException {
         XBee xbee = new XBee();
        
      try{
        //OPEN SERIAL PORT
            xbee.open("/dev/ttyUSB1", 9600);                 
            
            Scanner input = new Scanner(System.in);
            //XBeeAddress64 address64 = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x8b, 0x2e, 0x54); Mi xbee router
            XBeeAddress64 address64 = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x89, 0xec, 0x92); //xbee router Douglas
            int[] payload;
            payload = new int[1];
            
            System.out.println("Comando para el LED: 0 para APAGAR EL LED , 1 para ENCENDERLO");
            payload[0] = (int)input.nextByte();
                
            ZNetTxRequest request = new ZNetTxRequest(address64, payload);
            
            System.out.println("\nZB request is: " + request.getXBeePacket());

             while(true){

                try{
                    ZNetTxStatusResponse response = (ZNetTxStatusResponse) xbee.sendSynchronous(request, 100000);
 
                    request.setFrameId(xbee.getNextFrameId());

                    System.out.println("Response received"+ response);
                    
                    if (payload[0]==2){
                        
                        XBeeResponse resp = xbee.getResponse(500);
                        System.out.println("Entro a ver el estatus");
                        if(resp.getApiId() == ApiId.ZNET_RX_RESPONSE){
                            ZNetRxResponse rxResponse = (ZNetRxResponse)resp;
                            System.out.println("Received RX packet, option is " + rxResponse.getOption() + ", sender 64 address is " + ByteUtils.toBase16(rxResponse.getRemoteAddress64().getAddress()) + ", remote 16-bit address is " + ByteUtils.toBase16(rxResponse.getRemoteAddress16().getAddress()) + ", data is " + ByteUtils.toBase16(rxResponse.getData()));
                             int []x;
                             x=rxResponse.getData();
                             if(x[0]==10){
                                System.out.println("El foco se encuentra encendido");
                            }
                            else if (x[0]==20){
                                System.out.println("El foco se encuentra apagado");
                            }
                        else{
                            System.out.println("Hay un error");
                        }
                        }
                    }
                    
                    if(response.getDeliveryStatus() == ZNetTxStatusResponse.DeliveryStatus.SUCCESS){
                        System.out.println("Success!");
                    }else{
                        System.out.println("Packet failed due to" + response.getDeliveryStatus());
                    }
                }catch(XBeeTimeoutException e){
                System.out.println("Me ! Unable to send");
            }
                System.out.println("Enter a new command for LED: 0 to turn the LED OFF, 1 to turn on");
                payload[0] = (int)input.nextByte();
                
                request.setPayload(payload);
            }
      }finally{
          xbee.close();
      }
    }
}
XBee xbee = XBee();

You should not be calling the constructor directly.

XBee xbee;

will create an instance of the class, indirectly invoking the constrictor.

uint8_t payload[]={3};

Why are you using a one element array? A scalar variable would make more sense.

Given the little bit of data that you are sending, and the fact that the XBee doesn't seem to be doing anything other than acting as a radio, there seems to be no reason to be using API mode or the XBee library.

            XBeeAddress64 address64 = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x89, 0xec, 0x92); //xbee router Douglas

Mixing decimal and hexadecimal values when setting the address doesn't strike me as a good idea. Yes, I know that 0x00 and 0 are the same value, but it would certainly be clearer that the values represent an address if all the values were in hexadecimal.