Header Sep
Java Tips, Tricks & Code
 
My Rating Score
Login to rate page

June 2005
Part one: Using irDA with the OBEX protocol to create a client application

[Back]

Until recently, J2ME developers for Sony Ericsson mobile phones have been unable to use Infrared connectivity for their apps. However, the Java Platform 5 (JP-5) changes that by including JSR-82 as one of the supported features. The Sony Ericsson mobile phones included in JP-5 are the K750, W800 and the K600 series.
 
Infrared connectivity is available through the OBEX protocol which is part of the JSR-82 Bluetooth API.

We will discuss the OBEX protocol in two separate articles. Here in the first article, we present a brief introduction, outline how to create a client application and provide a code sample to download. The second article, available later this summer, we will discuss the server side of the protocol.

Introduction to the OBEX protocol
The OBEX protocol is used to transport data objects such as address book contacts and files between mobile devices. Today it is the standard for Infrared-equipped mobile devices and a part of the Bluetooth protocol hierarchy.

The OBEX protocol has a defined set of operations that are used for handling and sending data. It uses a client/server request-response model. An OBEX Client is the entity that initiates the connection to an OBEX server and starts OBEX operations.
The OBEX server waits for the OBEX client to initiate the connection and then responds to an OBEX operation.

There are eight operations that are defined in the OBEX protocol:

  • CONNECT: initiates the connection and sets up the basic expectations of each side of the link.
  • PUT: sends one object from the client to the server.
  • GET: the client requests that the server sends an object to the client.
  • SETPATH: changes the active directory on the server.
  • CREATE-EMPTY: creates an empty object on the server.
  • DELETE: removes an object from the server.
  • ABORT: terminates a multi-packet operation (such as PUT) before it would normally end.
  • DISCONNECT: signals the end of the OBEX session.

An OBEX session is started with the CONNECT operation and ended with the DISCONNECT operation. In between, the client can invoke any number of SETPATH, GET, PUT, CREATE-EMPTY or DELETE operations.

Information about the object can be sent with headers. There are a number of defined headers for this purpose, a couple of them are:

  • The Name header: a text string describing the name of the object, for example JUMAR.TXT.
  • The Type header: describes the type of the object, such as text, binary or electronic business card. Type is used by the receiving side to aid in intelligent handling of the object.
  • The Length header: sets the total length in bytes of the object. This allows the receiver to quickly terminate transfers requiring too much space, and also makes progress reporting easier. A PUT request, for instance, is normally used with the name and the length header.

Creating a client application

As the OBEX API is based on the Generic Connection Framework (GCF) it is easy to create a normal HTTP connection.To create a client connection, you simply use the Connector.open(String url) method.

The URL for OBEX should be formatted according to GCF framework: {protocol}:[{target}][{params}] where the protocol is set to "irdaobex".

The target can be "discover", "addr", "conn", or "name".

The "discover" target is used for searching for Infrared devices nearby and we will use this as we want to connect to any Sony Ericsson mobile phone in our immediate surroundings.
ClientSession session = (ClientSession) Connector.open("irdaobex://discover");

The Connector factory returns a ClientSession object which is the client-side connection object for OBEX. It extends the Connection interface and adds methods for OBEX requests and defines headers for the OBEX operations.

Use the ClientSession object to start a CONNECT operation.
HeaderSet head = session.connect(null);

The returned Headerset object is used to add headers for this session. First, tell the OBEX server the length of the message.
head.setHeader(head.LENGTH, new Long(messageBytes.length));

Then instruct the server that your message should be saved as JUMAR.TXT.
head.setHeader(HeaderSet.NAME, " JUMAR.TXT ");

We are now ready to initiate the PUT request with the defined header.
Operation op = session.put(head);

The returned Operation object extends the ContentConnection interface and adds functionality for header manipulation. Use it to open an OutputStream to the server.
OutputStream out = op.openOutputStream();

Now you can send your message as a byte array to the server.
out.write(new String("Hello there").getBytes());
out.flush();

Don't forget to end the session with the DISCONNECT operation and close the ClientSession,
session.disconnect(null);
session.close();

as well as the Operation and the OutputStream.
out.close();
op.close();

You can easily send your message as a calendar note or a contact by formatting the message and specifying the correct file type with the NAME header. For example if you want to send a calendar note containing the message "Hello there" you first have to set the name header to the calendar notes file type ".vnt":

head.setHeader(HeaderSet.NAME, "VNote.vnt");

This will make the receiving Sony Ericsson phone aware that there is a calendar note coming. Now format the message according to VNote format:

BEGIN:VNOTE
VERSION:1.1
BODY:Hello there
END:VNOTE

Now the message is ready to be sent.

Download code example>>

More information:

My Rating Score
Login to rate page