Header Sep
Symbian OS Tips, Tricks & Code
Score
Login to rate page
June 2004
Accessing Bluetooth from Java applications using JNI
 

[Back]

In this article, we'll be presenting a sample implementation of Bluetooth communication. This Bluetooth JNI library and sample application was created by Tage Borg from the Royal Institute of Technology in Sweden with support from Martin Svensson of the Swedish Institute of Computer Science. All code is provided "as is".

The Bluetooth communication code is written in C++ and is accessed from an application written in Java using JNI. The code is divided into three parts; the C++ Bluetooth library, an example Java client and an example Java server. The following zip files contain the example applications both as source code and as ready-made installation files suitable for either the P800 or P900.

Running the example
The file "sicsbtlib_applications.zip" contains installation files for both the client ("echoclient.SIS") and the server ("echoserver.SIS") and are ready for installation on either the P800 or P900 in the usual manner. Please note that two handsets will be required; one to act as the client and one to act as the server.

Make sure that Bluetooth is activated on both devices and that the device that will be acting as the server is set to "discoverable" mode ("Visible to other devices" in the case of a P900). When activated, a Bluetooth symbol should be displayed in the bottom of the screen when in "flip-open" mode and towards the top right hand corner when in "flip-closed" mode. Bluetooth can be activated using "Control panel --> Connections --> Bluetooth".

Echoserver is a Java application that implements a Bluetooth server. When started, the button "Start server" should be pressed to listen for incoming connections on port 15 using Bluetooth.

Echoclient is a Java application that can perform device discovery, open a connection to any nearby device found and send text messages. When the client has been started, the "Discover" button should be pressed. This will initiate Bluetooth device discovery so that other Bluetooth devices can be found. The discovery phase may take 30 seconds or more to complete, so please have some patience. When other Bluetooth devices have been found, they will be displayed in a list, ready for you to select one. The Bluetooth device with the echoserver should be chosen. Next, the "Connect" button should be pressed, as this will open a connection between the client and the server. Finally, the "Send" button can be pressed, as this will send a text string to the server, which will be echoed back to the client. All communication is wireless and uses the short-range Bluetooth radio communication standard.

Compiling the example
The file "sicsbtlib_code_and_examples.ZIP" contains the code for the example programs. In order to compile them, both the Java SDK 1.1.8 (found in the Java archives at Sun) and the UIQ SDK must be installed on the host PC with both Java and C++ enabled.

Detailed instructions for building the examples have been provided in the file "sicsbtlib_compile_guide.txt" which is included in the zip archive.

Interface to Bluetooth native methods in sicsbtlib
The interface between the Java and the C++ code is defined by the Java classes SicsBtLibSymbian, BTSocket, BTDevice, UUID and SDPRecord.

The SicsBtLibSymbian class includes the following public methods:

SicsBtLibSymbian()
void setupServer(SDPRecord rec)
void setupClient(SDPRecord rec)
BTSocket listenForIncoming(SDPRecord rec)
BTSocket listenForIncoming()
void disconnectClient()
void disconnectClient(BTSocket sock)
void disconnectServer()
void disconnectServer(BTSocket sock)
void closeServer()
void closeClient()
BTSocket connect(SDPRecord rec)
BTSocket guiConnect(SDPRecord rec)
BTDevice[] scan(int scanperiod, boolean deviceName, int maxDevs)

The BTSocket class includes the following public methods:
int write(byte[] b, int start, int length)
int read(byte[] b, int start, int length)

The SDPRecord class mainly contains information about the Bluetooth connection. BTDevice represents a specific Bluetooth device. 

Interface usage for client application
On the client side, a SicsBtLibSymbian object should be created. In order to find other devices to connect to, the scan() method should be used. This will return an array containing details of any nearby devices. When a device has been found, a connection can be opened to it by calling connect(). If the connection attempt succeeds, this will return a BTSocket object that can be used to read from and write to. To close the client, the end connection disconnectClient() is called.

Interface usage for server application
On the server side, a SicsBtLibSymbian object should be created. First setupServer() should be called and then listenForIncoming() can be called. When a client successfully opens a connection, this will return a BTSocket which is to be read from and written to using read() and write(). To close the connection from the server, disconnectServer() is called.

 
Score
Login to rate page