/**
   * Adds an output instance to the queue. The derived class should use this method for each output
   * instance it makes available.
   *
   * @param instance the instance to be added to the queue
   */
  protected void push(M5Instance instance) {

    if (instance != null) {
      copyStringValues(instance, m_OutputFormat, m_OutputStringAtts);
      instance.setDataset(m_OutputFormat);
      m_OutputQueue.push(instance);
    }
  }
  // Main thread
  public void run() {
    String stringBuffer = "";
    byte[] byteBuffer = new byte[1024];

    connection = Bluetooth.waitForConnection();
    connected = true;
    is = connection.openDataInputStream();
    os = connection.openDataOutputStream();
    terminateFlag = false;

    while (!terminateFlag) {
      // read into byte[] buffer
      int bytesRead;
      try {
        bytesRead = is.read(byteBuffer);
      } catch (IOException e) {
        bytesRead = 0;
      }
      if (bytesRead > 0) {
        // transfer from byte[] into StringBuffer
        stringBuffer += new String(byteBuffer, 0, bytesRead);
        // check for }
        // if found, this suggests that we just finished receiving a full message
        int endChar = stringBuffer.indexOf("}");
        if (endChar != -1) {
          // check for matching {
          int startChar = stringBuffer.indexOf("{");
          if (startChar != -1 && startChar < endChar) {
            // parse the message and add it to the queue
            Message messageRead = new Message(stringBuffer.substring(startChar, endChar + 1));
            messageQueue.push(messageRead);
            Message ack = new Message(messageRead.getSeqNum());
            ack.pairs.add(new String[] {"ack", null});
            sendMessage(ack);
          }

          // clean command up to } off stringBuffer
          if (endChar == stringBuffer.length() - 1) {
            stringBuffer = "";
          } else {
            stringBuffer = stringBuffer.substring(endChar + 1);
          }
        }
      }
    }

    // disconnect
    try {
      is.close();
      os.close();
    } catch (IOException e) {
      // TODO: not fail silently
    }
    connection.close();
    connected = false;
  }