lejos.nxt.comm
Class BTConnection

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

public class BTConnection
extends Object
implements NXTConnection

Provides a Bluetooth connection Supports both packetized and stream based commincation. 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.


Field Summary
static int AM_ALWAYS
           
static int AM_DISABLE
           
static int AM_OUTPUT
           
 
Constructor Summary
BTConnection(int chan)
           
 
Method Summary
 int available()
           
 int available(int what)
          Indicate the number of bytes available to be read.
 void close()
          Close the connection.
 void closeStream()
          Close the stream for this connection.
 byte[] getAddress()
           
 int getSignalStrength()
          Get the signal strength of this connection.
 DataInputStream openDataInputStream()
          Return the DataInputStream for this connect
 DataOutputStream openDataOutputStream()
          Return the DataOutputStream for this connection.
 InputStream openInputStream()
          Return the InputStream for this connection.
 OutputStream openOutputStream()
          Return the OutputStream for this connection
 void openStream()
          Open the stream for this connection.
 int read(byte[] data, int len)
           
 int read(byte[] data, int outLen, boolean wait)
          Attempt to read data from the connection.
 int readPacket(byte[] buf, int len)
          Read a packet from the stream.
 void sendPacket(byte[] buf, int bufLen)
          Send a data packet.
 void setActiveMode(int mode)
          Set the channel switching mode.
static void setInputBufferSize(int sz)
          Set the size to be used for the input buffer.
 void setIOMode(int mode)
          Set operating mode.
static void setOutputBufferSize(int sz)
          Set the size to be used for the output buffer.
 int write(byte[] data, int len)
           
 int write(byte[] data, int len, boolean wait)
          Attempt to write bytes to the Bluetooth connection.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait
 

Field Detail

AM_DISABLE

public static final int AM_DISABLE
See Also:
Constant Field Values

AM_ALWAYS

public static final int AM_ALWAYS
See Also:
Constant Field Values

AM_OUTPUT

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

BTConnection

public BTConnection(int chan)
Method Detail

getAddress

public byte[] getAddress()

close

public void close()
Close the connection. Flush any pending output. Inform the remote side that the connection is now closed. Free resources.

Specified by:
close in interface Connection

write

public int write(byte[] data,
                 int len,
                 boolean wait)
Attempt to write bytes to the Bluetooth connection. Optionally wait if it is not possible to write at the moment. Supports both packet and stream write opperations. If in packet mode a set of header bytes indicating the size of the packet will be sent ahead of the data. NOTE: If in packet mode and writing large packets (> 254 bytes), then the blocking mode (wait = true), should be used to ensure that the packet is sent correctly.

Specified by:
write in interface NXTConnection
Parameters:
data - The data to be written.
len - The number of bytes to write.
wait - True if the call should block until all of the data has been sent.
Returns:
> 0 number of bytes written. 0 Request would have blocked (and wait was false). -1 An error occurred -2 Data has been lost (See notes above).

write

public int write(byte[] data,
                 int len)
Specified by:
write in interface NXTConnection

read

public int read(byte[] data,
                int outLen,
                boolean wait)
Attempt to read data from the connection. Optionally wait for data to become available. Supports both packet and stream mode operations. When in packet mode the packet length bytes are automatically processed. The read will return just a single packet. If the packet is larger then the requested length then the rest of the packet will be returned in the following reads. If wait is true then in packet mode the call will wait until either the entire packet can be read or outLen bytes are available. In stream mode the call will return if at least 1 byte has been read.

Specified by:
read in interface NXTConnection
Parameters:
data - Location to return the data. If null the data is discarded.
outLen - Max number of bytes to read.
wait - Should the call block waiting for data.
Returns:
> 0 number of bytes read.

read

public int read(byte[] data,
                int len)
Specified by:
read in interface NXTConnection

available

public int available(int what)
Indicate the number of bytes available to be read. Supports both packet mode and stream connections.

Parameters:
what - 0 (all modes) return the number of bytes that can be read without blocking. 1 (packet mode) return the number of bytes still to be read from the current packet. 2 (packet mode) return the length of the current packet.

available

public int available()

setIOMode

public void setIOMode(int mode)
Set operating mode. Controls the packet/stream mode of this channel. For packet mode it defines the header size to be used.

Parameters:
mode - Size of header, 0 indicates stream mode.

readPacket

public int readPacket(byte[] buf,
                      int len)
Read a packet from the stream. Do not block and for small packets (< 254 bytes), do not return a partial packet.

Parameters:
buf - Buffer to read data into.
len - Number of bytes to read.
Returns:
> 0 number of bytes read. other values see read.

sendPacket

public void sendPacket(byte[] buf,
                       int bufLen)
Send a data packet. Must be in data mode.

Parameters:
buf - the data to send
bufLen - the number of bytes to send

setActiveMode

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.

Parameters:
mode - The switch control mode.

setInputBufferSize

public static void setInputBufferSize(int sz)
Set the size to be used for the input buffer. This will effect all new connections after this call is made.

Parameters:
sz - The required size. if < 0 the default size will be used

setOutputBufferSize

public static void setOutputBufferSize(int sz)
Set the size to be used for the output buffer. This will effect all new connections after this call is made.

Parameters:
sz - The required size. if < 0 the default size will be used

openInputStream

public InputStream openInputStream()
Return the InputStream for this connection.

Specified by:
openInputStream in interface InputConnection
Returns:
the input stream

openOutputStream

public OutputStream openOutputStream()
Return the OutputStream for this connection

Specified by:
openOutputStream in interface OutputConnection
Returns:
the output stream

openDataInputStream

public DataInputStream openDataInputStream()
Return the DataInputStream for this connect

Specified by:
openDataInputStream in interface InputConnection
Returns:
the data input stream

openDataOutputStream

public DataOutputStream openDataOutputStream()
Return the DataOutputStream for this connection.

Specified by:
openDataOutputStream in interface OutputConnection
Returns:
the data output stream

closeStream

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


openStream

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


getSignalStrength

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

Returns:
a value from 0 to 255