new Thread() {
public void run() {
try {
// the address book; it's easy to add other lists
ContactList addressbook =
(ContactList)( PIM.getInstance().openPIMList(
} catch (Exception ex) {
ex.printStackTrace();
}
}
}.start();
COMMUNITY
JAVA IN ACTION
Figure 3
Figure 4
createBackup(), although the latter is private and called only via the startBackup()
method.
The startBackup() method is called
either automatically when the MIDlet
starts (via your initiation or via the
PushRegistry alarm) or when you select
the Do Backup Now UI option.
As expected, the startBackup() method
first checks to see whether a backup
destination has been defined. If the destination has not been defined, it alerts
you that the backup can’t start. Next,
it creates a thread to do the backup in
a manner that will not block the main
thread. See Listing 6.
In the code in Listing 6, I have backed
up only the address book. It would be
easy to add a to-do list, a calendar list,
or any other data that the MIDlet may
Download all listings in this issue as text
compresses it as it goes along. The
actual looking up of the data is done in
the createBackup() method.
The code in Listing 7 goes through
the items in the address book, looks up
a valid data_format for serializing the
items (provided via the PIM API) and
iterates over individual items, while
adding these items to the compression stream. At the end, the stream is
flushed and the data written out.
Once the createBackup() method is finished, the startBackup() method uses the
doSend() method of the BluetoothServices
class we discussed earlier to actually
send the data to the connected device.
If you have established a trusted and
paired connection between your device
and its backup destination, you will get
the data in a pre-established Bluetooth
Exchange folder. I have tested this pro-
cess with a live device (Nokia N95) and
confirmed that it works perfectly. See
Figure 3, Figure 4, and Figure 5, which
show the activation of the PushRegistry,
the destination confirmation, and data
transfer confirmation on the target
device. </article>
ABOUT US
Figure 5
access (which, admittedly, is limited and
heavily implementation dependent).
To make the data transfer faster, I
used an external library to compress the
data before it is sent over the Bluetooth
channel. This library, called compress-
j2me, is extremely small and provides
good compression.
I start by creating two output streams:
one for holding the data and the other
to compress it. The compression stream
takes the first stream as the input and
blog
LEARN MORE
•;“Discovering Devices and Sending Files
via Bluetooth in Java ME”
•;The compress-j2me project