de.avetana.bluetooth.l2cap
Class L2CAPConnectionImpl

java.lang.Object
  extended byde.avetana.bluetooth.connection.BTConnection
      extended byde.avetana.bluetooth.l2cap.L2CAPConnectionImpl
All Implemented Interfaces:
Connection, L2CAPConnection

public class L2CAPConnectionImpl
extends BTConnection
implements L2CAPConnection

The implementation of the javax.bluetooth.L2CAPConnection class.

COPYRIGHT:
(c) Copyright 2004 Avetana GmbH ALL RIGHTS RESERVED.

This file is part of the Avetana bluetooth API for Linux.

The Avetana bluetooth API for Linux is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

The Avetana bluetooth API is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

The development of the Avetana bluetooth API is based on the work of Christian Lorenz (see the Javabluetooth Stack at http://www.javabluetooth.org) for some classes, on the work of the jbluez team (see http://jbluez.sourceforge.net/) and on the work of the bluez team (see the BlueZ linux Stack at http://www.bluez.org) for the C code. Classes, part of classes, C functions or part of C functions programmed by these teams and/or persons are explicitly mentioned.



Description:
This class is an implementation of the javax.bluetooth.L2CAPConnection class and opens a new L2CAP connection with a remote BT device

See Also:
L2CAPConnection

Field Summary
protected  int m_receiveMTU
           
protected  int m_transmitMTU
           
 
Fields inherited from class de.avetana.bluetooth.connection.BTConnection
closed, dataBuffer, fid, m_remote
 
Fields inherited from interface javax.bluetooth.L2CAPConnection
DEFAULT_MTU, MINIMUM_MTU
 
Constructor Summary
  L2CAPConnectionImpl(int fid)
          Creates a new instance of L2CAPConnectionImpl and set the connection ID
  L2CAPConnectionImpl(int fid, java.lang.String addr)
          Creates a new instance of L2CAPConnectionImpl.
protected L2CAPConnectionImpl(int fid, java.lang.String addr, int transmit, int receive)
          Creates a new instance of L2CAPConnectionImpl.
 
Method Summary
static L2CAPConnection createL2CAPConnection(JSR82URL url)
          Creates a new L2CAP connection with a remote BT device.
 int getReceiveMTU()
          Returns the ReceiveMTU that the connection supports.
 int getTransmitMTU()
          Returns the MTU that the remote device supports.
 boolean ready()
          Determines if there is a packet that can be read via a call to receive().
 int receive(byte[] inBuf)
          Reads a packet of data.
 void send(byte[] data)
          Requests that data be sent to the remote device.
 void setConnectionURL(JSR82URL a_url)
          Sets the connection URL.
 
Methods inherited from class de.avetana.bluetooth.connection.BTConnection
available, close, getConnectionID, getRemoteDevice, isClosed, newData, read, setRemoteDevice
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.microedition.io.Connection
close
 

Field Detail

m_transmitMTU

protected int m_transmitMTU

m_receiveMTU

protected int m_receiveMTU
Constructor Detail

L2CAPConnectionImpl

public L2CAPConnectionImpl(int fid)
Creates a new instance of L2CAPConnectionImpl and set the connection ID

Parameters:
fid - the connection ID

L2CAPConnectionImpl

public L2CAPConnectionImpl(int fid,
                           java.lang.String addr)
Creates a new instance of L2CAPConnectionImpl. Set the connection ID and the remote device

Parameters:
fid - The connection ID
addr - The address of the remote device

L2CAPConnectionImpl

protected L2CAPConnectionImpl(int fid,
                              java.lang.String addr,
                              int transmit,
                              int receive)
Creates a new instance of L2CAPConnectionImpl. Initializes the data buffer, the different MTU values and the connection properties (Remote device, connection ID)

Parameters:
fid - The connection ID
addr - The address of the remote device
transmit - The desired value of transmitMTU
receive - The desire value of receiveMTU
Method Detail

setConnectionURL

public void setConnectionURL(JSR82URL a_url)
Sets the connection URL.

Parameters:
a_url - The new connection URL

createL2CAPConnection

public static L2CAPConnection createL2CAPConnection(JSR82URL url)
                                             throws java.lang.Exception
Creates a new L2CAP connection with a remote BT device. All connection parameters (receiveMTU, transmitMTU,..etc..) are encapsulated in the connection URL.

Parameters:
url - The connection URL
Returns:
An instance of L2CAPConnection if the establishment of the connection did succeed.
Throws:
java.lang.Exception

getTransmitMTU

public int getTransmitMTU()
                   throws java.io.IOException
Returns the MTU that the remote device supports. This value is obtained after the connection has been configured. If the application had specified TransmitMTU in the Connector.open() string then this value should be equal to that. If the application did not specify any TransmitMTU, then this value should be less than or equal to the ReceiveMTU the remote device advertised during channel configuration.

Specified by:
getTransmitMTU in interface L2CAPConnection
Returns:
the maximum number of bytes that can be sent in a single call to send() without losing any data
Throws:
java.io.IOException - if the connection is closed

getReceiveMTU

public int getReceiveMTU()
                  throws java.io.IOException
Returns the ReceiveMTU that the connection supports. If the connection string did not specify a ReceiveMTU, the value returned will be less than or equal to the DEFAULT_MTU. Also, if the connection string did specify an MTU, this value will be less than or equal to the value specified in the connection string.

Specified by:
getReceiveMTU in interface L2CAPConnection
Returns:
the maximum number of bytes that can be read in a single call to receive()
Throws:
java.io.IOException - if the connection is closed

receive

public int receive(byte[] inBuf)
            throws java.io.IOException,
                   java.lang.NullPointerException
Reads a packet of data. The amount of data received in this operation is related to the value of ReceiveMTU. If the size of inBuf is greater than or equal to ReceiveMTU, then no data will be lost. Unlike read() on an java.io.InputStream, if the size of inBuf is smaller than ReceiveMTU, then the portion of the L2CAP payload that will fit into inBuf will be placed in inBuf, the rest will be discarded. If the application is aware of the number of bytes (less than ReceiveMTU) it will receive in any transaction, then the size of inBuf can be less than ReceiveMTU and no data will be lost. If inBuf is of length 0, all data sent in one packet is lost unless the length of the packet is 0.

Specified by:
receive in interface L2CAPConnection
Parameters:
inBuf - byte array to store the received data
Returns:
the actual number of bytes read; 0 if a zero length packet is received; 0 if inBuf length is zero
Throws:
java.io.IOException - if an I/O error occurs or the connection has been closed
java.io.InterruptedIOException - if the request timed out
java.lang.NullPointerException - if inBuf is null

ready

public boolean ready()
              throws java.io.IOException
Determines if there is a packet that can be read via a call to receive(). If true, a call to receive() will not block the application.

Specified by:
ready in interface L2CAPConnection
Returns:
true if there is data to read; false if there is no data to read
Throws:
java.io.IOException - if the connection is closed
See Also:
receive(byte[])

send

public void send(byte[] data)
          throws java.io.IOException
Requests that data be sent to the remote device. The TransmitMTU determines the amount of data that can be successfully sent in a single send operation. If the size of data is greater than the TransmitMTU, then only the first TransmitMTU bytes of the packet are sent, and the rest will be discarded. If data is of length 0, an empty L2CAP packet will be sent.

Specified by:
send in interface L2CAPConnection
Parameters:
data - data to be sent
Throws:
java.io.IOException - if data cannot be sent successfully or if the connection is closed
java.lang.NullPointerException - if the data is null