|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Java ME Wireless J2ME Applications with Java and Bluetooth - Part 2 of 2
Wireless J2ME Applications with Java and Bluetooth - Part 2 of 2
By: Bruce Hopkins
Sep. 1, 2002 12:00 AM
This article is the second installment in a two-part series on Java and Bluetooth. Last month you got your feet wet in Bluetooth (JDJ, Vol. 7, issue 8); if you don't remember what the role of a Bluetooth stack or a Bluetooth profile is in the context of a Bluetooth application, refer to Part 1. Let's recap. The basic concepts of any Bluetooth application (Java or otherwise) consist of the following components:
Device Management
That's about all it takes to perform device management with the Java Bluetooth Specification APIs. Now, let's look at the concept that allows you to discover other Bluetooth devices: device discovery.
Device Discovery
The two classes required for your Bluetooth device to discover remote Bluetooth devices in the area are DiscoveryAgent and DiscoveryListener. After getting a LocalDevice object, instantiate a DiscoveryAgent by calling LocalDevice.getDiscoveryAgent().
LocalDevice localdevice = LocalDevice.getLocalDevice(); The are multiple ways to discover remote Bluetooth devices, but I'll discuss one particular method. First, your object must implement the DiscoveryListener interface. This interface works like any listener, so it'll notify you when an event occurs. In this case, you'll be notified when Bluetooth devices are in the area. To start the discovery process, call the startInquiry() method on your DiscoveryAgent. This method is nonblocking, so you're free to do other things while you wait for other Bluetooth devices to be found. When a Bluetooth device is found, the JVM calls the deviceDiscovered() method of the class that implemented the DiscoveryListener interface. This method passes you a RemoteDevice object that represents the device discovered by the inquiry.
Service Discovery
That's where service discovery comes in. You can never be sure what services a RemoteDevice may offer, so service discovery helps you find out. Service discovery is just like device discovery - you use the DiscoveryAgent to do the "discovering." The searchServices() method of the DiscoveryAgent class allows you to search for services on a RemoteDevice. When services are found, the servicesDiscovered() method will be called by the JVM if your object implemented the DiscoveryListener interface. This callback method also passes in a ServiceRecord object that pertains to the service you searched for. With a ServiceRecord in hand, you can do plenty of things, but you would most likely want to connect to the RemoteDevice where this ServiceRecord originated: String connectionURL = servRecord[i].getConnectionURL(0,false);
Service Registration
Note: In a peer-to-peer application, such as a file transfer or chat application, any device can act as the client or the server, so you'll need to incorporate that functionality (both client and server) into your code in order to handle both scenarios of service discovery (the client) and service registration (the server). Here's an example of what's involved in getting your service registered and stored in the SDDB (Listing 1 provides the code; which can be downloaded from www.sys-con/java/sourcec.cfm): 1. Call Connector.open() and cast the resulting connection to a StreamConnectionNotifier. Connector.open() creates a new ServiceRecord and sets some attributes. 2. Use the LocalDevice object and the StreamConnectionNotifier to obtain the ServiceRecord that was created by the system. 3. Add or modify the attributes in the ServiceRecord (optional). 4. Use the StreamConnectionNotifier and call acceptAndOpen() and wait for Bluetooth clients to discover this service and connect. The system creates a service record in the SDDB. 5. Wait until a client connects. 6. When the server is ready to exit, call close() on the StreamConnectionNotifier. The system removes the service record from the SDDB. StreamConnectionNotifier and Connector come from the javax.microedition.io package of the J2ME platform. That's all that you need to do service registration in Bluetooth. The next step is communication.
Communication
Server Connections with the Serial Port Profile
Here are a few items that I want to point out: the string URL begins with btspp://localhost, which is required if you're going to use the Bluetooth Serial Port Profile. Next comes the UUID part of the URL, which is 0011223344556677889900AABBCCDDEEFF. This is simply a custom UUID that I made up for this service; I could have chosen any string that was either 32-bits or 128-bits long. Finally, we have ;name=serialconn in the URL string. I could have left this part off, but I want my custom service to have a name, so the actual service record in the SDDB has the following entry: ServiceName = serialconn The implementation also assigned a channel identifier to this service. The client must provide the channel number along with other parameters in order to connect to a server.
Client Connections with the Serial Port Profile
StreamConnection con = Obtain the URL string that you need to connect to the device from the ServiceRecord object you get from service discovery. The following is a more complete demonstration of how an SPP client makes a connection to an SPP server.
String connectionURL = What does an SPP client connection URL look like? If the address of the server is 0001234567AB, the string for the SPP client would look something like this: btspp://0001234567AB:3 The "3" at the end of the URL string is the channel number that the server assigned to this service when it was added to the SDDB.
Java Bluetooth Development Kits
The JSR-82 Reference Implementation was created by Motorola, so they're responsible for distributing it (www.motorola.com/java). Unfortunately, this Reference Implementation is only the JSR-82 APIs (see Figure 1, section B) so you won't be able to do much with it even if you have some Bluetooth hardware. To get the JSR-82 Reference Implementation to work on your development system, you need to license the Motorola CLDC as well. If you already have Bluetooth devices for your development computers, try out the JSR-82-compliant Java Bluetooth solution from Atinav (www.atinav.com). They support numerous RS-232, UART, USB, CF, and PCMCIA Bluetooth devices. Their solution is based on an all-Java stack and their SDK includes the following profiles: GAP, SDAP, SPP, and GOEP. This solution also includes a KVM, so you only need to obtain the Bluetooth hardware to create and test your apps. Rococo (www.rococosoft.com) is most famous for their Java Bluetooth simulator, although they also make a Java Bluetooth developer kit for the Palm OS. Using the Impronto Simulator, Java developers only need a single computer to compile and test their Java Bluetooth applications. The simulator supports GAP, SDAP, SPP, and GOEP profiles, and is currently priced at $1,000. Esmertec (www.esmertec.com) is well known for their Jbed Micro Edition CLDC implementation and the Jbed RTOS package. The Jbed platform is based upon their FastBCC technology, which dynamically loads and executes Java bytecode at native speeds (much like the goals of the Project Monty KVMs). One key advantage of Esmertec is that the Jbed CLDC implementation can run on a variety of different hardware platforms - with or without an operating system! Esmertec implemented their Bluetooth stack completely in Java, and they support GAP, SDAP, SPP, LAP, and PAN on a wide array of Bluetooth hardware modules. They also included support for Palm OS and Pocket PC devices. Smart Network Devices (www.smartnd.com) has a unique approach in their Java Bluetooth development kit; they include all the components shown in Figure 1, section B, for a complete Java Bluetooth-enabled device: a Bluetooth device (radio), a Bluetooth stack, Bluetooth profiles, a KVM, and the JSR-82 Java Bluetooth APIs. Their product is called Micro BlueTarget Starter Kit - Java version (see Figure 2), based on the Micro BlueTarget reference board. It supports the GAP, SDAP, SPP, and GOEP profiles. The Micro BlueTarget is a not a peripheral (like the 3Com Bluetooth module in Figure 3). It's a self-contained Java Bluetooth host device, so it doesn't need to be connected to a host computer to operate. It has its own operating system and KVM built in, and you can download your code to the device using its Ethernet or RS232 interface. You can buy the complete Java Bluetooth development kit for $2,880 and the Micro BlueTarget boards in single quantities for $425.
The Advantages of Java and Bluetooth
One member has a new program for his PDA called the Java Shared Whiteboard. Using Bluetooth technology, he sends that program to the rest of the team. Using Over-the-Air Provisioning (OTA) provided by J2ME, each member installs and runs the application on the fly. The meeting can now begin because the whiteboard is shared among the PDAs. Each participant can draw figures on his or her device and the image will appear instantly on the other screens. To save time, one member can take notes and send them to everyone's device while the meeting is in progress. What are the benefits of Java and Bluetooth? Of course, Java gives you platform independence for the Shared Whiteboard application. Therefore, you don't need to worry about what kind of PDA your fellow team members have (Palm, Pocket PC, Sharp, Sony, Handspring, etc.) as long as a compatible KVM and libraries are available. Bluetooth has an additional benefit - it enables you to create instant wireless networks with these devices in order to collaborate and share data. This network is portable, so you can move it to the cafeteria, a conference room, or outside - it doesn't matter.
Summary
Book Overview
YOUR FEEDBACK
LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK SPONSORED BY INFRAGISTICS
BREAKING JAVA NEWS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||