JavaBT

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:

Of course you also need the JavaBT jar which you can get from the Download section. You also have to add the path to the JavaBT jar in your Java SDK classpath to be able to use JavaBT in your project.

To be able to compile the JavaBT DLL the following software are needed:

Of course you also need the JavaBT source which you can get from the Download section.

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
}

Compiling the JavaBT DLL

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:

If the compile was successfull in every step then you can find the JavaBT DLL in the Symbian SDK build folder.

For Windows systems follow these steps:

If the compile was successfull in every step then you can find the JavaBT DLL in the Symbian SDK build folder.