In Part 1 we went over the basics of Bluetooth technology and some possible development opportunities Bluetooth gives us. Part 2 of this article goes into a little more detail how to create a Bluetooth server and client.
About the Example
In keeping things simplistic as possible proper handling of multiple slaves/clients is not implemented essentially it only works with one server and one client. As well the sample is just that sample code, it is coded from the perspective of getting the two nodes to communicate with each without any real effort in "proper" coding technique/design. Remember the focus is to get you started with the actual Java Bluetooth API.
Before we continue you should download the JSR 82 Java Doc API from www.bluetooth.org . When you run the program you will notice it outputs Major 512 and Minor 4, according to the chart it is a Phone / Celluar.
Server Overview
In the server class we need to initialize once again but this time instead of searching for devices we need to setup a server
private static String serverUrl = "btspp://localhost:" +
BluetoothEchoDemo.RFCOMM_UUID + ";name=rfcommtest;authorize=true";
.
.
.
conn = null;
localDevice = LocalDevice.getLocalDevice();
localDevice.setDiscoverable( DiscoveryAgent.GIAC );
notifier = (StreamConnectionNotifier)Connector.open(serverUrl);
.
.
.
Now make a connection with the same Generic Connector you use when making HTTP calls. The url definition is as follows:
scheme://host:port;parameters
Running the Example
Download the source and compile from here.
You will need to invoke the Sun WTK twice because the outputs are displayed in the consoles. Start one instance and select Server, then start a second instance and select Client (Yes you need to start 2 instances of the WTK not just simply hit the "run" button twice on the same instance because the console of the first instance will not show). Like HTTP calls you may need to answer yes for connections being made.
In the end you will see the following in the server console:
Starting Echo Server
Server Running...
Received Message from Client: Say Hello World
In the client console you will see:
Device Discovered
Major Device Class: 512 Minor Device Class: 4
Bluetooth Address: 000060854FBF
Bluetooth Friendly Name: WirelessToolkit
InquiryCompleted
ServicesDiscovered
SERVICE_SEARCH_COMPLETED
Service URL: btspp://000060854FBF:1;master=false;encrypt=false;authenticate=false
Hello Back from Server
Summary and What is Missing
You should be able to make a basic RFCOMM communication between a Bluetooth client and server. However, even though we went through a lot detail there are still details that were left out or not mentioned. Now that you have a better understanding of Bluetooth with J2ME you need to dive deeper into the following:
- An understanding of Service Discovery Database (SDDB)
- A more in depth look at UUIDs and other Data Elements
- Understand what a Service Record is and what it does
- Understanding the relationship between SDDB, Service Record and Data Elements
- BluetoothDemo comes with the WTK 2.2 Beta (Sun)
- BluePad comes with the SDK from SonyEricsson
- BlueToothCar comes with the SDK from SonyEricsson
- Samples that comes with the Rococo Software SDK Source Code http://java.sun.com (Wireless Toolkit 2.2 Beta includes JSR 82) http://developer.sonyericsson.com (J2ME SDK 2.1.4 Beta includes JSR 82) http://www.rococosoft.com http://www.smartnd.com http://jcp.org/aboutJava/communityprocess/final/jsr082/ http://www.bluetooth.org http://bluez.sourceforge.net/
Suggest a Correction
Found an error or have a suggestion? Let us know and we'll review it.





No comments yet. Be the first to comment!