Thursday, April 26, 2012

J2ME Bluetooth GPS Guide

J2ME Bluetooth GPS Guide

 

Bluetooth API JSR-82

The Java APIs for Bluetooth define the following package that depends on the CLDC javax.microedition.io package:
  • javax.bluetooth: core Bluetooth API
The above package is a separate optional package, which means that a CLDC implementation can include either package or both. MIDP-enabled devices are expected to be the kind of devices to incorporate this specification.

Application Programming

The anatomy of a Bluetooth application has five parts: stack initialization, device management, device discovery, service discovery, and communication.

Stack Initialization

The Bluetooth stack is responsible for controlling the Bluetooth device. The initialization process comprises a number of steps whose purpose is to get the device ready for wireless communication.

Device Management

The Java Bluetooth APIs contain the classes LocalDevice and RemoteDevice, which provide the device-management capabilities defined in the Generic Access Profile. LocalDevice depends on the javax.bluetooth.DeviceClass class to retrieve the device's type and the kinds of services it offers. The RemoteDevice class represents a remote device (a device within a range of reach) and provides methods to retrieve information about the device, including its Bluetooth address and name. The following code snippet retrieves that information for the local device:
...
// retrieve the local Bluetooth device object
LocalDevice local = LocalDevice.getLocalDevice();
// retrieve the Bluetooth address of the local device
String address = local.getBluetoothAddress();
// retrieve the name of the local Bluetooth device
String name = local.getFriendlyName();
...
You can get the same information about a remote device:
...
// retrieve the device that is at the other end of
// the Bluetooth Serial Port Profile connection,
// L2CAP connection, or OBEX over RFCOMM connection
RemoteDevice remote =
    RemoteDevice.getRemoteDevice(
        javax.microedition.io.Connection c);
// retrieve the Bluetooth address of the remote device
String remoteAddress = remote.getBluetoothAddress();
// retrieve the name of the remote Bluetooth device
String remoteName = local.getFriendlyName(true);
...
The RemoteDevice class also provides methods to authenticate, authorize, or encrypt data transferred between local and remote devices.

Device Discovery

Because wireless devices are mobile they need a mechanism that allows them to find other devices and gain access to their capabilities. The core Bluetooth API's DiscoveryAgent class and DiscoveryListener interface provide the necessary discovery services.
A Bluetooth device can use a DiscoveryAgent object to obtain a list of accessible devices, in any of three ways:
The DiscoveryAgent.startInquiry method places the device into an inquiry mode. To take advantage of this mode, the application must specify an event listener that will respond to inquiry-related events. DiscoveryListener.deviceDiscovered is called each time an inquiry finds a device. When the inquiry is completed or canceled, DiscoveryListener.inquiryCompleted is invoked.
If the device doesn't wish to wait for devices to be discovered, it can use the DiscoveryAgent.retrieveDevices method to retrieve an existing list. Depending on the parameter passed, this method will return either a list of devices that were found in a previous inquiry, or a list of pre-known devices that the local device has told the Bluetooth Control Center it will contact often.
These three code snippets demonstrate the various approaches:
...
// retrieve the discovery agent
DiscoveryAgent agent = local.getDiscoveryAgent();
// place the device in inquiry mode
boolean complete = agent.startInquiry();
...
...
// retrieve the discovery agent
DiscoveryAgent agent = local.getDiscoveryAgent();
// return an array of pre-known devices
RemoteDevice[] devices =
   agent.retrieveDevices(DiscoveryAgent.PREKNOWN);
...
...
// retrieve the discovery agent
DiscoveryAgent agent = local.getDiscoveryAgent();
// return an array of devices found in a previous inquiry
RemoteDevice[] devices =
    agent.retrieveDevices(DiscoveryAgent.CACHED);
...

Service Discovery

Once the local device has discovered at least one remote device, it can begin to search for available services - Bluetooth applications it can use to accomplish useful tasks. Because service discovery is much like device discovery, DiscoveryAgent also provides methods to discover services on a Bluetooth server device, and to initiate service-discovery transactions. Note that the API provides mechanisms to search for services on remote devices, but not for services on the local device.

Communication

For a local device to use a service on a remote device, the two devices must share a common communications protocol. So that applications can access a wide variety of Bluetooth services, the Java APIs for Bluetooth provide mechanisms that allow connections to any service that uses RFCOMM, L2CAP, or OBEX as its protocol. If a service uses another protocol (such as TCP/IP) layered above one of these protocols, the application can access the service, but only if it implements the additional protocol in the application, using the CLDC Generic Connection Framework.

Serial Port Profile

The RFCOMM protocol, which is layered over the L2CAP protocol, emulates an RS-232 serial connection. The Serial Port Profile (SPP) eases communication between Bluetooth devices by providing a stream-based interface to the RFCOMM protocol. Some capabilities and limitations to note:
  • Two devices can share only one RFCOMM session at a time.
  • Up to 60 logical serial connections can be multiplexed over this session.
  • A single Bluetooth device can have at most 30 active RFCOMM services.
  • A device can support only one client connection to any given service at a time.
For a server and client to communicate using the Serial Port Profile, each must perform a few simple steps.
As the following code snippet demonstrates, the server must:
  1. Construct a URL that indicates how to connect to the service, and store it in the service record
  2. Make the service record available to the client
  3. Accept a connection from the client
  4. Send and receive data to and from the client
The URL placed in the service record may look something like:
btspp://102030405060740A1B1C1D1E100:5
This says that a client should use the Bluetooth Serial Port Profile to establish a connection to this service, which is identified with server channel 5 on a device whose address is 102030405060740A1B1C1D1E100.
...
// assuming the service UID has been retrieved
String serviceURL =
    "btspp://localhost:"+serviceUID.toString());
// more explicitly:
String ServiceURL =
    "btspp://localhost:10203040607040A1B1C1DE100;name=SPP
        Server1";
try {
    // create a server connection
    StreamConnectionNotifier notifier =
      (StreamConnectionNotifier) Connector.open(serviceURL);
    // accept client connections
    StreamConnection connection = notifier.acceptAndOpen();
    // prepare to send/receive data
    byte buffer[] = new byte[100];
    String msg = "hello there, client";
    InputStream is = connection.openInputStream();
    OutputStream os = connection.openOutputStream();
    // send data to the client
    os.write(msg.getBytes());
    // read data from client
    is.read(buffer);
    connection.close();
} catch(IOException e) {
  e.printStackTrace();
}
...
At the other end, as the next code snippet shows, to set up an RFCOMM connection to a server the client must:
  1. Initiate a service discovery to retrieve the service record
  2. Construct a connection URL using the service record
  3. Open a connection to the server
  4. Send and receive data to and from the server
...
// (assuming we have the service record)
// use record to retrieve a connection URL
String url =
    record.getConnectionURL(
        record.NOAUTHENTICATE_NOENCRYPT, false);
// open a connection to the server
StreamConnection connection =
    (StreamConnection) Connector.open(url);
// Send/receive data
try {
    byte buffer[] = new byte[100];
    String msg = "hello there, server";
    InputStream is = connection.openInputStream();
    OutputStream os = connection.openOutputStream();
    // send data to the server
    os.write(msg.getBytes);
    // read data from the server
    is.read(buffer);
    connection.close();
} catch(IOException e) {
  e.printStackTrace();
}
...
How to read GPS data from Bluetooth stream

Code snippet
readBluetoothBuffer()
{
data = readAllAvailableBytes()
if (data.length == 0)
Bluetooth connection not there
else
Bluetooth seems OK
bytesRead = readBytes();
if (no bytesRead)
read error
else
START Loop
find newLine()
{
                    //figure out whether it's RMC or GGA
                    String candidate = readBuffer
                
                    //figure out whether RMC or GGA
                    if (candidate is $GPRMC....)
                   {
                        Store candidate as RMC
                    }
                    else if (candidate is $GPGGA)
                    {
Store candidate as GGA
                    }
            }
            END Loop
}
Common NMEA Sentence types
The following information describes the NMEA-0183 sentences transmitted by TeleType GPS receivers.
Sentence Description
$GPGGA Global positioning system fixed data
$GPGLL Geographic position - latitude / longitude
$GPGSA GNSS DOP and active satellites
$GPGSV GNSS satellites in view
$GPRMC Recommended minimum specific GNSS data
$GPVTG Course over ground and ground speed

Link : http://developer.att.com/developer/forward.jsp?passedItemId=3400032

 

Wednesday, April 4, 2012

Faster Data Transfer With Bluetooth and Contactless Communication

Faster Data Transfer With Bluetooth and Contactless Communicatio

 

Bluetooth technology allows two devices near each other to communicate at a maximum speed of 3 Mb per second. In the grand scheme of wireless communication, Bluetooth is roughly two times faster than the data throughput of a 3G wireless phone but still 10 to 20 times slower than today's Wi-Fi speeds.

More than one billion Bluetooth-enabled devices are currently on the market, and Bluetooth does a good job of transferring files that are smaller than 10 MB in size. However, due to the inherent nature of most wireless communication protocols, Bluetooth devices need to discover other Bluetooth devices, even if both devices are right next to each other. Finding a remote Bluetooth device is great, but what services does that remote Bluetooth device offer? If the remote device is a printer, does it offer the commonly used Basic Printing Profile (BPP) or the more advanced printing service known as the Hard Copy Cable Replacement Profile (HCRP)?
To find the available services on a remote Bluetooth device, you also need to search for a service. Bluetooth device-discovery and service-searching capabilities are great when you are trying to find any remote Bluetooth device in the vicinity that can suit your needs.
But device discovery and service searching are extremely time-consuming and frustrating to use when you're trying to communicate with a device that's right in front of you. This article shows you how to get Bluetooth applications to completely bypass the device-discovery and service-searching processes simply by using Near-Field Communication (NFC) technology and JSR 257: Contactless Communication API.
Contents

Introduction to NFC Technology
Introducing JSR 257: Contactless Communication API
Basic Bluetooth or NFC + Bluetooth: Which Is Faster?
Do Things Faster: Add NFC to the Picture
Conclusion
For More Information

Introduction to NFC Technology

What exactly is NFC technology? NFC is a radio communication standard that enables wireless data transfer between two devices at a very short distance -- less than 10 centimeters. NFC devices include a certain class of radio-frequency identification (RFID) tags and contactless smart cards. NFC devices operate within the 13.56 MHz frequency range, and they support extremely low data rates -- a maximum of 0.42 Mb per second.
Compared to Bluetooth and Wi-Fi, NFC technology operates at drastically reduced transfer rates and only within a very small proximity. If that's the case, why use NFC technology? Here are three reasons:
  • Setup time. NFC devices communicate instantly -- in less than 100 milliseconds -- when placed within range.
  • Power consumption. NFC tags and cards do not consume power, so their lifespan can be unlimited.
  • Cost. NFC tags and cards are inexpensive to manufacture compared to other wireless technologies.
NFC technology has easily met the need for certain use cases in the industry, such as the following:
  • Wireless payments. Traditional smart cards have been used for cashless payments for years. The short setup time for NFC devices allows mobile payments to be even easier.
  • Smart magazines and posters. Want to get more information about something you read in a magazine or see on a poster on the street? Just tap the page to get more information, or get the URL to store the bookmark for later use.
  • Transit tickets. When used in scenarios for mass transit, NFC-enabled phones are simple and convenient.
  • Business-card exchange. Because setup times for communication between NFC devices are very short, NFC devices are ideal for business-card exchanges.
Table 1 compares NFC with other wireless communication technologies.

Table 1: A Comparison of Wireless Communication Standards

International Standard
Operating Frequency
Maximum Data Rate
Maximum Distance
Power Consumption Rate
Bluetooth 3.0
IEEE 802.15.1
2.4 GHz
22 Mb/s
100 m
hours/days
Bluetooth 2.1
IEEE 802.15.1
2.4 GHz
3 Mb/s
100 m
days
NFC
ISO 14443
13.56 MHz
0.42 Mb/s
10 cm
Wi-Fi B
IEEE 802.11b
2.4 GHz
11 Mb/s
100 m
hours
Wi-Fi G
IEEE 802.11g
2.4 GHz
54 Mb/s
100 m
hours
Wi-Fi N
IEEE 802.11n
5 / 2.4 GHz
144 Mb/s
100 m
hours
ZigBee
IEEE 802.15.4
2.4 GHz
0.25 Mb/s
100-1200 m
months/years

As you can see, NFC technology is definitely not suited to transferring large amounts of data over long distances -- that is definitely a job for other wireless communication protocols such as any of the flavors of Wi-Fi.
But if you want to provide a small amount of information in a very short amount of time, then NFC may be for you. And a Java technology API is available that allows developers to interact with NFC devices: the JSR 257 API.

Introducing JSR 257: Contactless Communication API

JSR 257, the Contactless Communication API, is the Java ME API that allows mobile phones to communicate with a variety of devices within proximity. The Contactless Communication API allows your Java ME MIDlets to read and write data to the following entities:
  • External contactless smart cards
  • NFC tags
  • Generic RFIDs
  • 2D barcodes
  • Other NFC-capable phones
  • Internal secure elemen
Link: http://java.sun.com/developer/technicalArticles/javame/nfc_bluetooth/