FileDocCategorySizeDatePackage
BluetoothConnection.javaAPI DocphoneME MR2 API (J2ME)9210Wed May 02 18:00:32 BST 2007com.sun.kvem.jsr082.bluetooth

BluetoothConnection

public abstract class BluetoothConnection extends Object
Base class for all bluetooth connections.

Fields Summary
protected com.sun.midp.io.BluetoothUrl
url
Keeps requested connection details.
protected int
mode
Keeps open mode.
private boolean
authorized
true if this connection was authorized, false otherwise.
private boolean
encrypted
true if this connection has requested encryption and is encrypted.
private javax.bluetooth.RemoteDevice
remoteDevice
Remote device for this connection.
Constructors Summary
protected BluetoothConnection(com.sun.midp.io.BluetoothUrl url, int mode)
Creates a new instance of this class.

param
url connection url
param
mode I/O access mode server side otherwise it's false

        // IMPL_NOTE: find proper place; the intent here is to start EmulationPolling
        // and SDPServer prior to create a user's notifier
        SDDB.getInstance();

        this.url = url;
        this.mode = mode;
    
Methods Summary
public booleanauthorize()
Authorizes this connection. It is assumed that the remote device has previously been authenticated. This connection must represent the server side, i.e. isServer() should return true.

return
true if the operation succeeded, false otherwise

        authorized = BCC.getInstance().authorize(
                remoteDevice.getBluetoothAddress(), getServiceRecordHandle());
        return authorized;
    
protected voidcheckOpen()
Checks if this connection is open.

throws
IOException if this connection is closed

        if (isClosed()) {
            throw new IOException("Connection is closed.");
        }
    
protected voidcheckReadMode()
Checks read access.

throws
IOException if open mode does not permit read access

        if ((mode & Connector.READ) == 0) {
            throw new IOException("Invalid mode: " + mode);
        }
    
protected voidcheckSecurity()
Performs security checks, such as authentication, authorization, and encryption setup.

throws
BluetoothConnectionException when failed

        if (url.authenticate) {
            if (!remoteDevice.authenticate()) {
                throw new BluetoothConnectionException(
                        BluetoothConnectionException.SECURITY_BLOCK,
                        "Authentication failed.");
            }
        }
        if (url.authorize) {
            if (!remoteDevice.authorize((Connection)this)) {
                throw new BluetoothConnectionException(
                        BluetoothConnectionException.SECURITY_BLOCK,
                        "Authorization failed.");
            }
        }
        if (url.encrypt) {
            if (!remoteDevice.encrypt((Connection)this, true)) {
                throw new BluetoothConnectionException(
                        BluetoothConnectionException.SECURITY_BLOCK,
                        "Encryption failed.");
            }
        }
    
protected voidcheckWriteMode()
Checks write access.

throws
IOException if open mode does not permit write access

        if ((mode & Connector.WRITE) == 0) {
            throw new IOException("Invalid mode: " + mode);
        }
    
public booleanencrypt(boolean enable)
Changes encryption for this connection.

param
enable specifies whether encription should be turned on or off
return
true if encryption has been set as required, false otherwise

        if (enable == encrypted) {
            return true;
        }
        BCC.getInstance().encrypt(remoteDevice.getBluetoothAddress(), enable);
        if (remoteDevice.isEncrypted()) {
            if (enable) {
                encrypted = true;
                return true;
            }
            encrypted = false;
            return false;
        } else {
            if (enable) {
                return false;
            }
            encrypted = false;
            return true;
        }
    
public static com.sun.kvem.jsr082.bluetooth.BluetoothConnectiongetConnection(javax.microedition.io.Connection conn)
Retrieves BluetoothConnection from given one. Connection given is supposed to be either Bluetooth connection or a connection that uses Bluetooth as transport. All involved connections supposed to be open.

param
conn the connection to extract Bluetooth connection from
return
proper BluetoothConnection instance
throws
IllegalArgumentException if connection is neither an instance of BluetoothConnection, nor uses one as transport.
throws
IOException if connection given or transport is closed or transport is invalid.

        if (conn == null) {
            throw new NullPointerException("Null connection specified.");
        }

        if (conn instanceof ObexPacketStream) {
            conn = ((ObexPacketStream)conn).getTransport();
        }

        if (!(conn instanceof BluetoothConnection)) {
            throw new IllegalArgumentException("The specified connection " +
                    "is not a Bluetooth connection.");
        }

        BluetoothConnection btConn = (BluetoothConnection)conn;
        btConn.checkOpen();
        return btConn;
    
public javax.bluetooth.RemoteDevicegetRemoteDevice()
Returns remote device for this connection.

return
RemoteDevice object for this connection
throws
IOException if this connection is closed

        checkOpen();
        return remoteDevice;
    
public abstract java.lang.StringgetRemoteDeviceAddress()
Returns Bluetooth address of the remote device for this connection.

return
Bluetooth address of the remote device

protected intgetServiceRecordHandle()
Returns handle for the service record of the service this connection is attached to. Valid for server-side (incoming) connections only.

return
service record handle, or 0 if the handle is not available

        return 0;
    
public booleanisAuthorized()
Returns the authorization state of this connection.

return
true if this connection has been authorized, false otherwise

        return authorized;
    
public booleanisClosed()
Determines if this connection is closed.

return
true if this connection is closed, false otherwise

        return remoteDevice == null;
    
public booleanisServerSide()
Determines whether this connection represents the server side, i.e. this connection was created by a notifier in acceptAndOpen().

return
true if this connection is a server-side connection, false otherwise

        return url.isServer;
    
protected voidresetRemoteDevice()
Removes reference to the remote device.

        if (encrypted) {
            encrypt(false);
        }
        remoteDevice = null;
        BCC.getInstance().removeConnection(getRemoteDeviceAddress());
    
protected voidsetRemoteDevice()
Retrieves reference to the remote device for this connection.

        remoteDevice = DiscoveryAgentImpl.getInstance().
                getRemoteDevice(getRemoteDeviceAddress());
        BCC.getInstance().addConnection(getRemoteDeviceAddress());