| Home | About | News | Members | Publications | Download | Tutorial | Problems |
JavaBT Tutorial
Required Software and Knowledge
We assume that persons who want to use and especially compile our API has basic knowledge in development of Symbian programs and know how to build and compile software for Symbian. If not we suggest that you read a book about Symbian OS programming. If you want to compile the API then some basic knowlege in handling Ant build files is recommended. Also basic knowledge in creating and handling sis files is recommended.
To be able to compile Java programs that can use the JavaBT API DLL you need:
- A working Java SDK that can compile Personal Java programs with JNI support. e.g Java 1.1
To be able to compile the JavaBT DLL the following software are needed:
- A working Symbian SDK for Sony Ericsson PXXX series smartphones.
- A working Java SDK that can compile Personal Java programs with JNI support. e.g Java 1.1
- A working version of Apache Ant installed.
Installation
To Install the binary distribution of JavaBT in your smartphone just download the sis file and install it in your smartphone. To be able to use the sis version of JavaBT in your own project you also have to add the path to the JavaBT.jar in your projects classpath. The default path for JavaBT.jar in our sis is \system\apps\JavaBT\JavaBT.jar so just add that to your symbian project classpath. If you have compiled the DLL and jar by your self then it is recomended to build a sis of them an then install the sis.
How to use JavaBT
To use JavaBT in an application import the JavaBT packages:
import se.sics.bt.*; import se.sics.bt.symbian.*;
Start the JavaBT API
The first thing we need is to create a BTScanner and a BTCommander. The BTScanner needs a BTCommander to work. The BTCommander throws an exception if it failed to initialize the Symbian OS Bluetooth system. The class that uses the BTScanner need to implement the BTScanObserver interface.
// Starts a new BTCommander
try{
mCommander = new BTCommander();
}
catch(Exception e){
mInfo.setText("Could not initiate Bluetooth");
// No bluetooth connection, exit
System.exit(0);
}
// Create a new Bluetooth scanner
scanner = new BTScanner(mCommander, this);
/* --------------------------------------
BTScanObserver interface
--------------------------------------*/
public void deviceFound(BTDevice dev);
public void scanComplete();
public void notifyError(int error);
No we can start using Bluetooth, first we create a simple message passing client. The client will search for devices and connect to the devices that runs the "JavaBT" server. If a connection is made the client will send a message to the server and wait for an acknowledgement from the server.
Scanning for devices
Scanning for nearby Bluetooth devices is very simpel just activate the BTScanner and give it a timeout. Calling the scanner is an asynchronous call therefor we need to wait unti the result has returned. The activeDevice variable is set in one of the two callback functions. The callback functions also resumes the thread after the call to wait().
// search for a new device
mInfo.insertText("\nSearching ",
mInfo.getText().length());
scanner.scanDevice(20);
// Wait for the scanner to return a device, or timeout
try{
wait();
} catch (Exception e){};
// Check if we got a device or not
if(activeDev == null){
mInfo.insertText("\nSearch ended! ",
mInfo.getText().length());
return;
} else {
// A new device is found
}
}
/**
* Callback:
* Returns a new device.
*/
public void deviceFound(BTDevice dev){
short[] addr = dev.getAddr();
activeDev= dev;
// Notify the client thread
synchronized(this){
notify();
}
}
/**
* Callback:
* On a scan complete.
*/
public void scanComplete(){
// Notify the client thread
synchronized(this){
activeDev = null;
notify();
}
}
Connect to a service or port
There are two ways to connect to a device, direct to a port or to a service. In this example we connect to a service called "JavaBT". If we connect to a service we do ont need to know the protocol (L2CAP/RFCOMM) or port/channel number. Doing a direct connection requires a protocol and port number.
// Connect to the JavaBT service
socket1 = activeDev.connect("JavaBT");
/* Or do a direct connect to a given port.
socket1 = activeDev.connect(port,protocol, 10);
*/
// check if the connection was succeeded
if(socket1 == null){
mInfo.insertText("\nError connecting ",
mInfo.getText().length());
} else {
//We got a socket connection
}
To be able to compile the JavaBT DLL your system need to fullfill the Requirements. When you have both the SDKs working and the source tree on your local machine then you can compile the DLL. First you need to alter Ants build file: <src root>/group/build.xml to make the EPOCROOT variable point to where you have your own Symbian SDK installed.
For Linux systems follow these steps:
- Change directory to: <src root>/group/
- Build the Java classes: ant linux
- Create Symbian build files: bldmake bldfiles
- Execute the JNI build script: ./makejniexports.mk
- Compile the Symbian C++ source files: abld build armi urel
For Windows systems follow these steps:
- Change directory to: <src root>\group\
- Build the Java classes: ant
- Create Symbian build files: bldmake bldfiles
- Execute the JNI build script: makejniexports
- Compile the Symbian C++ source files: abld build armi urel