Class BTConnection

  extended by lejos.nxt.comm.NXTConnection
      extended by lejos.nxt.comm.BTConnection
All Implemented Interfaces:
Connection, InputConnection, OutputConnection, StreamConnection

public class BTConnection
extends NXTConnection

Provides a Bluetooth connection Supports both packetized, raw and stream based communication. Blocking and non-blocking I/O. Notes: Because of the limited buffer space and the way that several connections have to share the interface to the Bluetooth device data may be lost. This will happen if a switch into command mode is required when there is data arriving from the remote connection that can not be placed into the input buffer. Every attempt is made to avoid this but it can happen. Application programs can help avoid this problem by: 1) Using just a single Bluetooth connection 2) Using Bluetooth commands while data transfers are in progress. 3) Performing application level flow control to avoid more then 256 bytes of data being sent from the remote side at any one time. 4) Reading any pending data as soon as possible. If data is lost then calls to read and write will return -2 to indicate the problem. If using packet mode then the input stream can be re-synchronized by issuing a read to discard the partial packet which may be in the input buffer. When operating in RAW mode bytes are read/written as is. This mode is useful for talking to none leJOS/Lego devices. When operating in PACKET mode the standard Lego 2 byte header is added to each packet (and is expected to be present on each incoming packet). Use this mode when talking to other leJOS/Lego devices.

Field Summary
static int AM_ALWAYS
static int AM_DISABLE
static int AM_OUTPUT
Fields inherited from class lejos.nxt.comm.NXTConnection
Constructor Summary
BTConnection(int chan)
Method Summary
 void closeStream()
          Close the stream for this connection.
 int getSignalStrength()
          Get the signal strength of this connection.
 void openStream()
          Open the stream for this connection.
 void setActiveMode(int mode)
          Set the channel switching mode.
Methods inherited from class lejos.nxt.comm.NXTConnection
available, available, close, getAddress, openDataInputStream, openDataOutputStream, openInputStream, openOutputStream, read, read, readPacket, sendPacket, setIOMode, write, write
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Detail


public static final int AM_DISABLE
See Also:
Constant Field Values


public static final int AM_ALWAYS
See Also:
Constant Field Values


public static final int AM_OUTPUT
See Also:
Constant Field Values
Constructor Detail


public BTConnection(int chan)
Method Detail


public void setActiveMode(int mode)
Set the channel switching mode. Allows control of when we will switch to this channel. By default we will switch to this channel to check for input. However if AM_OUTPUT is set we only switch if we have output waiting to be sent.

mode - The switch control mode.


public void closeStream()
Close the stream for this connection. This suspends the connection and switch the BC4 chip to command mode.


public void openStream()
Open the stream for this connection. This resumes the connection and switches the BC4 chip to data mode.


public int getSignalStrength()
Get the signal strength of this connection. This necessitates closing and reopening the data stream.

a value from 0 to 255