public static void main(final String[] args) {
    try {
      final NXTComm device = connect("Lenny");

      final InputStream stream = device.getInputStream();

      final int length = (1 << 13);
      final byte[] buffer = new byte[length];
      final long start = System.currentTimeMillis();

      double diff;
      long last = 0, total = 0;
      for (int n; (n = stream.read(buffer, 0, length)) != -1; ) {
        if (n > 0) {
          total += n;
          if ((System.currentTimeMillis() - last) >= 1000L) {
            diff = ((last = System.currentTimeMillis()) - start);
            System.out.printf("%s%n", (1000D * total / diff));
          }
        }
      }
    } catch (final Throwable e) {
      e.printStackTrace();
    }
  }
 private static final NXTComm connect(final String name) throws NXTCommException {
   final NXTComm nxtc = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
   final NXTInfo[] devices = nxtc.search(name);
   if (devices.length != 1) {
     throw new NXTCommException("Unable to identify NXT brick");
   }
   nxtc.open(devices[0]);
   return nxtc;
 }
  @Override
  public void run() {
    try {
      communicator = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
      synchronized (listeners) {
        for (LinkListener listener : listeners) {
          listener.bluetoothInitialized(this);
        }
      }
    } catch (Throwable t) {
      t.printStackTrace();
      synchronized (listeners) {
        for (LinkListener listener : listeners) {
          listener.bluetoothFailed(this);
        }
      }
    }

    long lastSearchTime = 0;
    while (!stopRequest) {

      long currentTime = System.currentTimeMillis();

      if (!isConnected()
          && (availableRovers == null
              || (availableRovers.length == 0 && currentTime - lastSearchTime > 5000))) {
        try {
          synchronized (listeners) {
            for (LinkListener listener : listeners) {
              listener.searchStarted(this);
            }
          }
          availableRovers = communicator.search(null, NXTCommFactory.BLUETOOTH);
          synchronized (listeners) {
            for (LinkListener listener : listeners) {
              listener.searchCompleted(this);
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
          synchronized (listeners) {
            for (LinkListener listener : listeners) {
              listener.searchFailed(this);
            }
          }
        }
        lastSearchTime = System.currentTimeMillis();
      }

      if (transmitter != null && !transmitter.isConnected()) {
        transmitter = null;
        synchronized (messagesToSend) {
          messagesToSend.clear();
        }
        synchronized (listeners) {
          for (LinkListener listener : listeners) {
            listener.disconnected(this);
          }
        }
      }

      if (isConnected()) {
        transmitter.encodeMessage(new KeepaliveMessage());

        while (messagesToSend.size() > 0) {
          Message message = null;
          synchronized (messagesToSend) {
            message = (Message) messagesToSend.remove(0);
          }
          transmitter.encodeMessage(message);
        }

        // Check for connection timeout.
        if (transmitter.getLastReceiveTime() != 0
            && System.currentTimeMillis() - transmitter.getLastReceiveTime() > 5000) {
          transmitter.disconnect();
        }
      }

      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }