lejos.nxt.comm
Class BTConnection

java.lang.Object
  extended by lejos.nxt.comm.BTConnection
All Implemented Interfaces:
StreamConnection

public class BTConnection
extends Object
implements StreamConnection

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.
 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 setIOMode(int mode)
          Set operating mode.
 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

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 StreamConnection

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.

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)

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.

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)

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

openInputStream

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

Specified by:
openInputStream in interface StreamConnection
Returns:
the input stream
Throws:
IOException

openOutputStream

public OutputStream openOutputStream()
                              throws IOException
Return the OutputStream for this connection

Specified by:
openOutputStream in interface StreamConnection
Returns:
the output stream
Throws:
IOException

openDataInputStream

public DataInputStream openDataInputStream()
                                    throws IOException
Return the DataInputStream for this connect

Specified by:
openDataInputStream in interface StreamConnection
Returns:
the data input stream
Throws:
IOException

openDataOutputStream

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

Specified by:
openDataOutputStream in interface StreamConnection
Returns:
the data output stream
Throws:
IOException

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