code: private void scheduleSendTask(boolean runNow) {
          TimerTask task = new TimerTask() {
              @Override
              public void run() {
  
                  byte[] packet;
  
                  try {
                      packet = packets.get(0);
  
                  } catch (IndexOutOfBoundsException e) {
                      // No packets left; return without scheduling another run
                      return;
  
                  }
  
                  charc.setValue(packet);
                  boolean result = gattClient.writeCharacteristic(charc);
  
                  if (result) {
                      packets.remove(0);
                      int id = ids.remove(0);
                      retries = 0;
  
                      if (onPacketSent != null) {
                          onPacketSent.onResult(id);
                      }
                      Log.d(TAG, "Packet " + id + " sent after " + retries + " retries");
  
                  } else {
                      retries++;
  
                  }
  
                  scheduleSendTask(false);
              }
          };
  
          if (runNow) {
              task.run();
  
          } else {
              sendTimer.schedule(task, SEND_INTERVAL);
  
          }
      }
docs: Schedules the send task to run either immediately or at SEND_INTERVAL.
  
  @param runNow true runs the task immediately, false schedules it for SEND_INTERVAL ms from
  now