FileDocCategorySizeDatePackage
BTSPPConnectionImpl.javaAPI DocphoneME MR2 API (J2ME)18236Wed May 02 18:00:30 BST 2007com.sun.midp.io.j2me.btspp

BTSPPConnectionImpl

public class BTSPPConnectionImpl extends com.sun.kvem.jsr082.bluetooth.BluetoothConnection implements javax.microedition.io.StreamConnection
Bluetooth Serial Port Profile connection implementation.

Fields Summary
byte[]
remoteDeviceAddress
Stores the address of the remote device connected by this connection. The value is set by the constructor.
private final Object
readerLock
Lock object for reading from the socket
private final Object
writerLock
Lock object for writing to the socket
private int
handle
Identidies this connection at native layer, -1 if connection is not open.
private boolean
isOpened
Flag to identify if an input stream is opened for this connection.
private boolean
osOpened
Flag to identify if an output stream is opened for this connection.
private int
objects
Open streams counter.
Constructors Summary
protected BTSPPConnectionImpl(com.sun.midp.io.BluetoothUrl url, int mode)
Constructs an instance and opens connection.

param
url keeps connection details
param
mode I/O access mode
exception
IOException if connection fails

    
                              
        
              
        this(url, mode, null);
    
protected BTSPPConnectionImpl(com.sun.midp.io.BluetoothUrl url, int mode, BTSPPNotifierImpl notif)
Constructs an instance and sets up corresponding native connection handle to it.

param
url keeps connection details
param
mode I/O access mode
param
notif corresponding BTSPPNotifierImpl instance temporary storing native peer handle
exception
IOException if connection fails

        super(url, mode);

        if (notif == null) {
            remoteDeviceAddress = BluetoothUtils.getAddressBytes(url.address);
            doOpen();
        } else {
            remoteDeviceAddress = new byte[6];
            System.arraycopy(notif.peerAddress, 0,  remoteDeviceAddress, 0, 6);

            setThisConnHandle0(notif);
        }

        setRemoteDevice();
    
Methods Summary
private native intavailable0()
Returns the number of bytes available to be read from the connection without blocking. Note: the method gets native connection handle directly from handle field of BTSPPConnectionImpl object.

return
the number of available bytes
throws
IOException if any I/O error occurs

public voidclose()
Closes this connection.

throws
IOException if I/O error.

        synchronized (this) {
            if (isClosed()) {
                return;
            }
            resetRemoteDevice();
            objects--;
            if (objects == 0) {
                close0();
            }
        }
    
private native voidclose0()
Closes client connection. Note: the method gets native connection handle directly from handle field of L2CAPConnectionImpl object.

throws
IOException if any I/O error occurs

private native voidconnect0(byte[] addr, int cn)
Starts client connection establishment. Note: the method gets native connection handle directly from handle field of BTSPPConnectionImpl object.

param
addr bluetooth address of device to connect to
param
cn Channel number (CN) value
throws
IOException if any I/O error occurs

private native voidcreate0(boolean auth, boolean enc, boolean master)
Creates a client connection object. Note: the method gets native connection handle directly from handle field of BTSPPConnectionImpl object.

param
auth true if authication is required
param
enc true indicates what connection must be encrypted
param
master true if client requires to be a connection's master
throws
IOException if any I/O error occurs

private voiddoOpen()
Opens client connection.

        /*
         * create native connection object
         * Note: the method <code>create0</code> sets resulting native
         * connection handle directly to the field <code>handle<code>.
         */
        create0(url.authenticate, url.encrypt, url.master);

        byte[] address = BluetoothUtils.getAddressBytes(url.address);

        try {
            // establish connection
            connect0(address, url.port);
        } catch (IOException e) {
            throw new BluetoothConnectionException(
                BluetoothConnectionException.FAILED_NOINFO,
                e.getMessage());

        }
    
private native voidfinalize()
Native finalizer. Releases all native resources used by this connection.

public java.lang.StringgetRemoteDeviceAddress()
Retrieves remote address for the connection.

return
remote address

        return BluetoothUtils.getAddressString(remoteDeviceAddress);
    
private static native voidinitialize()
Native static class initializer.

public final java.io.DataInputStreamopenDataInputStream()
Open and return a data input stream for a connection.

return
An input stream
throws
IOException if an I/O error occurs

        return new DataInputStream(openInputStream());
    
public final java.io.DataOutputStreamopenDataOutputStream()
Open and return a data output stream for a connection.

return
An output stream
throws
IOException if an I/O error occurs

        return new DataOutputStream(openOutputStream());
    
public final java.io.InputStreamopenInputStream()
Open and return an input stream for a connection.

return
An input stream
throws
IOException if an I/O error occurs

        checkOpen();
        checkReadMode();
        synchronized (this) {
            if (isOpened) {
                throw new IOException("No more input streams");
            }
            isOpened = true;
            objects++;
        }
        return new SPPInputStream();
    
public final java.io.OutputStreamopenOutputStream()
Open and return an output stream for a connection.

return
An output stream
throws
IOException if an I/O error occurs

        checkOpen();
        checkWriteMode();
        synchronized (this) {
            if (osOpened) {
                throw new IOException("No more output streams");
            }
            osOpened = true;
            objects++;
        }
        return new SPPOutputStream();
    
private native intreceive0(byte[] buf, int off, int size)
Reads data from a packet received via Bluetooth stack. Note: the method gets native connection handle directly from handle field of BTSPPConnectionImpl object.

param
buf the buffer to read to
param
off the start offset in array buf at which the data to be written
param
size the maximum number of bytes to read, the rest of the packet is discarded.
return
total number of bytes read into the buffer, 0 indicates end-of-data, -1 if there is no data available at this moment
throws
IOException if an I/O error occurs

private native intsend0(byte[] buf, int off, int size)
Sends the specified data via Bluetooth stack. Note: the method gets native connection handle directly from handle field of L2CAPConnectionImpl object.

param
buf the data to send
param
off the offset into the data buffer
param
size the size of data in the buffer
return
total number of send bytes, or -1 if nothing is send
throws
IOException if an I/O error occurs

private native voidsetThisConnHandle0(BTSPPNotifierImpl notif)
Retrieves native connection handle from temporary storage inside BTSPPNotifierImpl instance and sets it to this BTSPPConnectionImpl instance. Note: the method sets native connection handle directly to handle field of BTSPPConnectionImpl object.

param
notif reference to corresponding BTSPPNotifierImpl instance storing native peer handle