The btsppEcho MIDlet implements a simple client-server pair for sending messages using the Bluetooth RFCOMM protocol.
The user can start the MIDlet in either the client (discoverable) or server (discovery) mode in the opening SettingsList screen. The opening screen also allows the user to select the appropriate security settings (authentication, encryption, and authorization), and whether general (GIAC) or limited (LIAC) inquiry access codes are used.
Selecting BT properties on the opening screen shows the Bluetooth properties of the device.
Figure: Bluetooth properties
When the user starts the MIDlet in the client mode, the MIDlet creates an instance of a ConnectionService object and displays the ClientForm screen.
The ConnectionService runs continuously, waiting to accept new connections on the appropriate server connection string. When it accepts a new connection from a remote server, it notifies the ClientForm that it has accepted a new connection and created a new ClientConnectionHandler to handle that connection. The ClientConnectionHandler is runnable, so it can read and write from the appropriate input and output streams.
The ClientForm starts a thread for each accepted ClientConnectionHandler, which causes the ClientConnectionHandler to instantiate the needed InputStream and OutputStream objects and to start both a reader and a writer thread for them.
The ClientForm implements the ClientConnectionHandlerListener interface, and accepts callbacks from its ClientConnectionHandler objects. The callbacks are related to opening the input and output streams, received messages, sent messages, and normal or abnormal connection closes. The ClientForm directly uses a ClientConnectionHandler to send messages.
Figure: Client view
When the user starts the MIDlet in the server mode, the MIDlet displays the ServiceDiscoveryList screen. From this screen, the user can start a search (device inquiry and service discovery), which returns the matching devices and services.
If the user selects to open the first connection to a remote client, the ServerForm screen is displayed. The ServerForm creates the actual connection to a remote client, and maintains a list of ServerConnectionHandler objects, one of which is created for each connection. The ServerConnectionHandler objects are runnable, and separate threads are used in each for writing to output and reading from input streams. The ServerForm is responsible for starting these threads when it creates a new ServerConnectionHandler object.
The ServerForm implements the ServerConnectionHandlerListener interface, and accepts callbacks from its ServerConnectionHandler objects. The callbacks are related to opening the input and output streams, received messages, sent messages, and normal or abnormal connection closes. The ServerForm directly uses a ServerConnectionHandler to send messages.
The ServerForm has an Add connection command, which temporarily causes a screen transition back to the ServiceDiscoveryList screen to select the next client to open a connection to.
Figure: Server view
For information about implementing the MIDlet, see section Implementation.