ch.aplu.bluetooth
Class BluetoothPeer

java.lang.Object
  extended by ch.aplu.bluetooth.BluetoothPeer

public class BluetoothPeer
extends java.lang.Object

Class that implements a Bluetooth peer-to-peer communication based on the client-server model. When started, an existing server is searched using a specified Bluetooth service. If found, the connection is establish in a client mode. Otherwise a server mode with the given service name is started.

The Bluetooth names of each remote device have to be added manually to the known Bluetooth device list using the Lejos command processor.

Data is exchanged as blocks of 1..100 integers.


Constructor Summary
BluetoothPeer(java.lang.String nodeName, BtPeerListener listener)
          Creates a BluetoothPeer instance that will connect to the given node.
 
Method Summary
 boolean isConnected()
          Returns the connection status.
 void releaseConnection()
          Releases a connection and inform the connected node's listener by calling notifyConnection(false).
 boolean sendDataBlock(int[] data)
          Send a block of data to the connected node.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BluetoothPeer

public BluetoothPeer(java.lang.String nodeName,
                     BtPeerListener listener)
Creates a BluetoothPeer instance that will connect to the given node. Starts in client mode and tries to connect to the given node that must run in server mode. If fails, start in server mode waiting for a incoming client connection. In both cases, the constructor does not block.

Parameters:
nodeName - the partner's Bluetooth name, if null, starts in server mode
listener - the BtPeerListener that gets connnection and data notifications
Method Detail

sendDataBlock

public boolean sendDataBlock(int[] data)
Send a block of data to the connected node. Data are queued and the method blocks when the queue is filled (queue space about 1000 ints).

Parameters:
data - the data block to send (size 1..100)
Returns:
true, if successful; false, if not connected, invalid data array (length 0) or stream write exception

isConnected

public boolean isConnected()
Returns the connection status.

Returns:
true, if connected, otherwise false

releaseConnection

public void releaseConnection()
Releases a connection and inform the connected node's listener by calling notifyConnection(false). If no connection is established, a waiting server is canceled.