Package communication

Source Code of communication.MasterConnection

package communication;

import BasicDataType.BlockingQueue;
import javax.bluetooth.RemoteDevice;
import lejos.nxt.comm.Bluetooth;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.NXTConnection;
/**
* Connect as master to the other (slave) NXT nodes.
*
* @version 1 2010-01-27
* @author Assar Lindstrom & Erik Lagercrantz
*/
public class MasterConnection extends Connection {
  /**Constructor
   * @param remoteNode
   *            is the Node that the robot becomes master of.
   * @param receiveQueue
   *            packet receiving queue.
   */
  public MasterConnection(Node remoteNode, BlockingQueue<Packet> receiveQueue) {
    this.remoteNode = remoteNode;
    this.receiveQueue = receiveQueue;
    this.sendQueue = new BlockingQueue<Packet>();
  }

  /**
   * Connects to node as master
   */
  public BTConnection connect(){
    BTConnection btc = null;

    RemoteDevice btrd = remoteNode.getRemoteDevice();

    int iCount = 0;
    while (true) {
      if(iCount==10) {
        iCount = 3;
      }
      else {
        iCount++;
      }
      if (btrd == null) {
        System.out.println("No such device!");
        continue;
      }

      btc = Bluetooth.connect(btrd);


      if (btc == null) {
        System.out.println("Connect "+remoteNode.getName()+" failed");
        try{
          Thread.sleep(iCount*1000);
        }catch(Exception e) {
          System.out.println("sleep problems");
        }
        continue;
      }
      else{
        System.out.println("Connected to "+remoteNode.getName()+".");
        btc.setIOMode(NXTConnection.RAW);
        break;
      }
    }
   
    return btc;
  }
}
TOP

Related Classes of communication.MasterConnection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.