org.openuat.util
Class BluetoothRFCOMMChannel

java.lang.Object
  extended by org.openuat.util.BluetoothRFCOMMChannel
All Implemented Interfaces:
RemoteConnection

public class BluetoothRFCOMMChannel
extends java.lang.Object
implements RemoteConnection

This is a very simple class that uses the JSR82 API to open an RFCOMM channel to a Bluetooth device.

Version:
1.0
Author:
Rene Mayrhofer

Nested Class Summary
static class BluetoothRFCOMMChannel.RDevice
          The RemoteDevice constructor taking a Bluetooth MAC address is, for some weird reason, protected.
protected static class BluetoothRFCOMMChannel.TempHandler
          Switches the state of the local device between Master and Slave
 
Constructor Summary
BluetoothRFCOMMChannel(java.lang.String connectionURL)
          Construct a Bluetooth RFCOMM channel object with a specific remote endpoint.
BluetoothRFCOMMChannel(java.lang.String remoteDeviceAddress, int remoteChannelNumber)
          Construct a Bluetooth RFCOMM channel object with a specific remote endpoint.
 
Method Summary
 void close()
          Closes the channel to the endpoint given to the constructor.
 boolean equals(java.lang.Object other)
          This implementation of equals either compares either the connection objects (if set) or serviceURL.
 java.io.InputStream getInputStream()
          Returns the InputStream object for reading from the remote Bluetooth device.
static BluetoothRFCOMMChannel[] getOpenChannels()
          Returns all BluetoothRFCOMMChannel objects whose channel is currently open or is being tried to be opened.
 java.io.OutputStream getOutputStream()
          Returns the OutputStream object for writing to the remote Bluetooth device.
 java.lang.Object getRemoteAddress()
          Implementation of RemoteConnection.getRemoteAddress.
 int getRemoteChannelNumber()
          Returns the remote channel number.
 java.lang.String getRemoteName()
          Implementation of RemoteConnection.getRemoteName.
 int hashCode()
          Override hashCode so as to provide the same integer when two objects are equal as defined by the overrode equals implementation.
 boolean isOpen()
          Implementation of RemoteConnection.isOpen.
static void main(java.lang.String[] args)
           
 boolean open()
          Opens a channel to the endpoint given to the constructor.
 void setRemoteChannelNumber(int remoteChannelNumber)
          Sets the remote channel number that will be used in the next open() call.
static void shutdownAllChannels()
          This method can be used to speed up shutdown of the overall application by closing all RFCOMM channels that are still open at this time.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BluetoothRFCOMMChannel

public BluetoothRFCOMMChannel(java.lang.String connectionURL)
                       throws java.io.IOException
Construct a Bluetooth RFCOMM channel object with a specific remote endpoint. This does not yet open the channel, @see open needs to be called for that.

Parameters:
connectionURL - The complete Bluetooth service URL, as returned e.g. by a service search.
Throws:
java.io.IOException - When the local Bluetooth stack was not initialized properly.

BluetoothRFCOMMChannel

public BluetoothRFCOMMChannel(java.lang.String remoteDeviceAddress,
                              int remoteChannelNumber)
                       throws java.io.IOException
Construct a Bluetooth RFCOMM channel object with a specific remote endpoint. This does not yet open the channel, @see open needs to be called for that. Note: This constructor, along with a dummy remoteChannelNumber (i.e. -1), can be used as a reference object to compare against, e.g. when querying KeyManager for a key with a remote Bluetooth device.

Parameters:
remoteDeviceAddress - The Bluetooth MAC address to connect to, in format "AABBCCDDEEFF".
remoteChannelNumber - The SDP RFCOMM channel number to connect to, usually between 1 and 10.
Throws:
java.io.IOException - When the local Bluetooth stack was not initialized properly.
Method Detail

getOpenChannels

public static BluetoothRFCOMMChannel[] getOpenChannels()
Returns all BluetoothRFCOMMChannel objects whose channel is currently open or is being tried to be opened.

Returns:
The array may be of length 0 when no channels are open, but will be != null.

shutdownAllChannels

public static void shutdownAllChannels()
This method can be used to speed up shutdown of the overall application by closing all RFCOMM channels that are still open at this time.


open

public boolean open()
             throws java.io.IOException
Opens a channel to the endpoint given to the constructor. The method is synchronized, because opening might block for some time and we don't want to do it twice for the same host.

Specified by:
open in interface RemoteConnection
Returns:
true if the connection could be (re-)opened, false otherwise.
Throws:
java.io.IOException - On Bluetooth errors.
java.io.IOException - When the channel has already been opened.

close

public void close()
Closes the channel to the endpoint given to the constructor. It may be re-opened with another call to @see #open. It is also an mplementation of RemoteConnection.close.

Specified by:
close in interface RemoteConnection
Throws:
java.io.IOException - On Bluetooth errors.
java.io.IOException - When the channel has not yet been opened.
See Also:
RemoteConnection.close

isOpen

public boolean isOpen()
Implementation of RemoteConnection.isOpen.

Specified by:
isOpen in interface RemoteConnection
Returns:
true if the connection can be used for sending and receiving, false if either (or both) of the channels is unusable (closed).

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Returns the InputStream object for reading from the remote Bluetooth device. It is also an mplementation of RemoteConnection.getInputStream.

Specified by:
getInputStream in interface RemoteConnection
Returns:
The InputStream object openend in @see #open.
Throws:
java.io.IOException - When the channel has not yet been opened.
See Also:
RemoteConnection.getInputStream

getOutputStream

public java.io.OutputStream getOutputStream()
                                     throws java.io.IOException
Returns the OutputStream object for writing to the remote Bluetooth device. It is also an mplementation of RemoteConnection.getOutputStream.

Specified by:
getOutputStream in interface RemoteConnection
Returns:
The OutputStream object openend in @see #open.
Throws:
java.io.IOException - When the channel has not yet been opened.
See Also:
RemoteConnection.getOutputStream

getRemoteAddress

public java.lang.Object getRemoteAddress()
Implementation of RemoteConnection.getRemoteAddress.

Specified by:
getRemoteAddress in interface RemoteConnection
Returns:
A String object with the remote Bluetooth device address. It would be nicer to return a RemoteDevice object in every case, but we can't due to the RemoteDevice(String) constructor being protected in the JSR81 API. Go complain to its authors.
See Also:
RemoteConnection.getRemoteAddress

getRemoteName

public java.lang.String getRemoteName()
Implementation of RemoteConnection.getRemoteName.

Specified by:
getRemoteName in interface RemoteConnection
See Also:
RemoteConnection.getRemoteName

getRemoteChannelNumber

public int getRemoteChannelNumber()
Returns the remote channel number.


setRemoteChannelNumber

public void setRemoteChannelNumber(int remoteChannelNumber)
Sets the remote channel number that will be used in the next open() call.


equals

public boolean equals(java.lang.Object other)
This implementation of equals either compares either the connection objects (if set) or serviceURL.

Specified by:
equals in interface RemoteConnection
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Override hashCode so as to provide the same integer when two objects are equal as defined by the overrode equals implementation. In this case, simply return the hashcode of the Bluetooth MAC address (as String), or 0 if no address is known.

Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException,
                        java.lang.NumberFormatException,
                        java.lang.InterruptedException
Throws:
java.io.IOException
java.lang.NumberFormatException
java.lang.InterruptedException


2005-2009, Rene Mayrhofer.