2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


Wireless J2ME Applications with Java and Bluetooth
Wireless J2ME Applications with Java and Bluetooth

This is Part 1 of a two-part article that will show you how to write wireless J2ME applications using Bluetooth. If you're a J2ME developer, this will quite likely be your first introduction to Bluetooth. Perhaps you've heard a lot about it, but you're not sure what it is or how it works. Well, you've come to the right place.

First, we'll look at the Bluetooth protocol, then compare it to another common wireless technology: 802.11b. Next, we'll look at the anatomy of a Java Bluetooth­enabled device and examine its components in detail. Later on, we'll dive into the details and explain the purpose of the Bluetooth stack and profiles (not to be confused with J2ME profiles). Finally, we'll wrap up by providing some example code that shows how to initialize your Bluetooth stack according to the Java APIs for Bluetooth (JSR-821.0a).

What Is Bluetooth?
Simply stated, Bluetooth is a wireless communication protocol that you'd use to connect two or more Bluetooth-capable devices. In this sense it's like any other communication protocol, such as HTTP, FTP, SMTP, or IMAP. Bluetooth is also similar to these protocols in that it has a client/server architecture. In Bluetooth, the one who initiates the connection is master (the client), and the one who receives the connection (the server) is the slave. However, what makes Bluetooth so special is that it's wireless. You can connect two Bluetooth devices to share data or transfer files without using messy cables.

Bluetooth vs 802.11b
If you've heard of Bluetooth, you've certainly heard of 802.11b (the wireless LAN protocol) ­ both of which are wireless communication protocols. Bluetooth and 802.11b are geared to accomplish two different goals, although the technologies operate in the same frequency band: 2.4GHz. The question many people ask is: If both technologies operate at the same frequency, won't they interfere when placed in range with each other? Not so, according to a Forrester Research study in 2001.

Another common question: If they both do the same thing, won't one eventually replace the other? The key point is that they're not intended for the same purpose. Bluetooth will replace 802.11b (or vice versa) when, for example, the microwave replaces the conventional oven.

Microwave vs Oven
The oven is great at cooking, but bad at heating food quickly ­ and it's also pretty expensive. Conversely, the microwave is cheap and great at heating food quickly, but it's bad at cooking. Both devices have their trade-offs, although either could be used for heating and cooking. How does all this compare to wireless communication?

It's pretty simple. Wireless LAN (802.11b) is good at connecting two relatively large devices with lots of power at high speeds. A good use of the technology is connecting two laptops at 11Mb/s. Wireless LAN is also good at connecting those devices at long distances (up to 300 ft).

Bluetooth is ideal if you want to connect small devices at slower speeds (1Mb/s) and within a shorter range (30 ft.). Why slower speeds? Isn't faster better? Isn't long-range communication a good thing? Not necessarily, especially when you consider the memory and power constraints on smaller devices. Faster connection speeds and longer communication ranges equate to larger power requirements. Why use an 11Mb/s connection if you're transferring a 50K file between two PDAs?

One of Bluetooth's strengths is its ability to function as a cable replacement technology. If you have multiple peripherals connected to your computer using RS-232 or USB, then Bluetooth is the ideal solution if you want to use those devices wirelessly. It's difficult (if not impossible) to connect computer peripherals using 802.11b technology (except for printers). Bluetooth even has a built-in capability for wireless audio communication. To put things succinctly: Bluetooth will never replace 802.11b because it's bad for:

  • Large file transfers between devices
  • Long-range communication
On the other hand, 802.11b will never replace Bluetooth because:
  • It can't be used to communicate to peripherals.
  • It requires too much power for small devices.
  • It's overkill for small data transfers.
  • It wasn't designed for voice communication.
For the moment in the wireless communications arena, there's no technology that's best suited for every possible application. Either Bluetooth or 802.11b can be utilized for wireless communication between computers. Both have their place in the market, and both can perform remarkably well in their niches.

Bluetooth Specification
For Bluetooth devices to communicate properly, they need to conform to the Bluetooth Specification. This specification, like any other spec, defines the standard that a Bluetooth device should adhere to, as well as rules that need to be enforced when communicating. You can download the specification documents from www.bluetooth.com.

Java Bluetooth Application Anatomy
Let's look at the anatomy of a Java Bluetooth application. Figure 1, section A, shows a Bluetooth-enabled phone that includes the CLDC MIDP platform; section B contains the anatomy of the device shown in section A. The bottom layer of the diagram in section B represents the actual Bluetooth device that sends out your voice and data over the air: the Bluetooth radio. Section A shows the Bluetooth device as a peripheral, but it could easily be integrated into the device by the OEM. Bluetooth radios come in many shapes and sizes; Figure 2 shows the popular Bluetooth radio made by 3Com. 3Com also makes a handy network browser with their Bluetooth device (see Figure 3).

The Bluetooth Protocol Stack
The next essential component of a Java Bluetooth application is the Bluetooth stack, (again, see Figure 1, section B). The Bluetooth stack is the software layer with direct access to the Bluetooth radio as well as control over such things as device settings, communication parameters, and power levels. The stack consists of many layers, and each layer has a specific task in the overall Bluetooth device (see Figure 4). The Bluetooth device manufacturer is not required to use all the layers, so let's cover the main ones that should be in most Bluetooth devices.

  • Host Controller Interface (HCI): The interface between the radio and the host computer

  • Logical Link Control and Adaptation Protocol (L2CAP): The multiplexer of all data passing through the unit; audio signals, however, have direct access to the HCI

  • Service Discovery Protocol (SDP): Used to find services on remote Bluetooth devices

  • RFCOMM: Widely known as the virtual serial port protocol

  • OBEX: Object exchange protocol

    Bluetooth Profiles
    The next level in Figure 1, section B, is Bluetooth profiles. They were created to allow different Bluetooth devices to interoperate. For instance, let's say you own a Bluetooth-enabled PDA and a Bluetooth-enabled wireless phone. Both devices have Bluetooth stacks. How can you tell if those two devices will allow you to synchronize the phone lists between each one? How will you know if you can send a phone number from the PDA to the phone? And, most important, how can you determine if these devices will allow you to browse the Internet on the PDA using the phone as a wireless modem?

    A Bluetooth profile is a designed set of functionality for Bluetooth devices. For instance, using the examples listed above, the phone and the PDA must both support the Synchronization Profile in order to synchronize data between themselves. To send object data like a .vcf from the PDA to the phone, both devices need to have the Object Push Profile implemented. Finally, the PDA and the wireless phone must both support the Dialup Networking Profile in order for the PDA to wirelessly browse the Internet from the phone. If you want your Bluetooth-enabled devices to interact, having a Bluetooth stack is not good enough: they need to conform to a particular profile.

    Bluetooth Profiles vs J2ME Profiles
    Do not confuse Bluetooth profiles with J2ME profiles. J2ME profiles are a set of Java classes that extend the functionality of a J2ME configuration. For example, both the PDA and MID profiles are a set of Java classes that extend the functionality of the Connected Limited Device Configuration.

    A Bluetooth profile can be implemented in any language and on any platform because it refers to a defined set of functionality for a Bluetooth-enabled device. So the Object Push Profile can be implemented on a Palm OS PDA in C++ as well as on a Bluetooth-enabled printer in Assembler; it's just a defined set of functionality.

    The next two layers of software in Figure 1, section B, are the KVM and the Java Bluetooth API libraries. With J2ME and the standard Java Bluetooth implementation, you have direct access to the profiles and the stack of your Bluetooth device. The Java Bluetooth API (JSR-82) works with any Java platform that supports the Generic Connection Framework (GCF). Unfortunately, that means you can't use J2SE with the official Java Bluetooth API. Hopefully, the GCF will be incorporated into JDK 1.5. Until then, you'll have to use nonstandard libraries if you want to write Java Bluetooth code for desktop applications. The last layer in Figure 1, section B, is your application code, the wireless killer app that will make you famous.

    Java Bluetooth Application Concepts
    The basic concepts of any Bluetooth application (Java or otherwise) consist of the following components:

    • Stack initialization
    • Device management
    • Device discovery
    • Service discovery
    • Service registration
    • Communication
    The Java Bluetooth Specification adds a special component to the mix called the Bluetooth Control Center (BCC), which is outside the scope of this article.

    Stack Initialization
    Before you can do anything, you need to initialize your stack. Remember, the stack is the piece of software that controls your Bluetooth device. Stack initialization can consist of a number of things, but its main purpose is to get the Bluetooth device ready to start wireless communication. Every vendor handles stack initialization differently; Listing 1 shows how to initialize the stack using the Atinav Java Bluetooth SDK.

    Summary
    Hopefully, this article has stimulated your interest in application development with Java and Bluetooth. Now that the preliminaries are out of the way, in Part 2 we can dive into more application code that will show you how to wirelessly communicate between two Java Bluetooth­enabled devices. Don't worry if you don't have any Bluetooth equipment yet; I'll also show where and how to obtain the hardware and software you'll need.

    About Bruce Hopkins
    Bruce Hopkins is a Senior Java Consultant at Great Lakes Technologies Group in Southfield, MI. He has worked in Java for over 6 years, and has researched in wireless networking for 4 years. He's the co-author of an upcoming book entitled Java Bluetooth by Apress (Nov. 2002), and can be reached at javaspaces@comcast.net

  • YOUR FEEDBACK
    Ethan wrote: check this out http//www.myspace.com/coolethanph
    roman_mino wrote: Mr Bruce, This was the thing i was looking for. It is the best introduction to bluetooth tecnology for a BT newbie like me.I like to do a project in BT and was looking for some useful link on the subject to start with.Thanks and congratulations for this great work. roman
    Marcus Rocha wrote: Mr. Bruce Hopkins, Congratulations for your article about wireless J2ME applications using Bluetooth. I'm student of Computer Networks and I'm studying about Bluetooth technology. Would you have any suggestion about a point for my monography in wireless J2ME applications using Bluetooth? I don't work with Java yet, but I want. Thank you. Marcus Rocha
    LATEST JAVA STORIES & POSTS
    "Ruby on Rails is always evolving and has over the past five years gone through some fifty-plus public releases," said David Heinemeier Hansson, creator of Rails and member of the Rails Core Team. "Rails developers need the supporting ecosystem to keep up with that evolution. New...
    "The Internet marketplace has evolved within and well beyond the traditional Web browser, fueled by an explosion of Java-powered smart phones and consumer electronic devices from RIM's Blackberry to Amazon's Kindle," said Jonathan Schwartz, CEO & President of Sun, as JavaFX 1.0 w...
    Worldwide server shipments might have been up 4.4% to 2.3 million units in the September quarter but revenues were down 5.4% to $12.7 billion according to Gartner. IDC's numbers aren't all that different. It hasn't been this bad since 2002 and IDC for one doesn't expect a rebound...
    Unit testing is hard. There I said it. Although I have been developing software for the past 18 years I still find that putting my applications through their paces via unit testing is difficult. I have learned the lesson (I'm sure like many of you) the hard way. Unit testing is p...
    Continuent has announced support and enhancements to MySQL Server 5.1.30 GA release, the 5.1 production version of the open source database. MySQL 5.1.30 is recommended for use on production systems by the MySQL build team at Sun Microsystems. Continuent Tungsten provides advance...
    As a software journalist, there are times when certain vendors will shut the door on reporting opportunities that might represent too much of an "inside view" of their technology or their organization. I've been to more developer events than I can remember where I've been handed ...
    SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
    SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
    Click to Add our RSS Feeds to the Service of Your Choice:
    Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
    myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
    Publish Your Article! Please send it to editorial(at)sys-con.com!

    Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


    SYS-CON FEATURED WHITEPAPERS

    SPONSORED BY INFRAGISTICS
    In every field of design one of the first things students do is learn from the work of others. They ...
    There are many forces that influence technological evolution. After a decade of building enterprise ...
    2008 is going to be an important year for Rich Internet Applications. Most organizations are deliver...
    The OpenAjax Alliance is developing an Ajax industry wishlist for future browsers, using a dedicated...
    Infragistics announced the availability of two Community Technology Preview (CTP) User Interface (UI...
    The YUI development team has released version 2.5.2; you can download the new release from SourceFor...
    ADS BY GOOGLE