Java Reference
In-Depth Information
Listing 15-10. Defining a New NDEF Record
public static void writeMessage()
{
try {
PIM pim = PIM.getInstance();
ContactList cl =
(ContactList)pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
Enumeration items = cl.items();
Contact item = (Contact) items.nextElement();
String name = item.getString(Contact.FORMATTED_NAME,0);
NDEFRecord[] records = new NDEFRecord[1];
records[0]=newNDEFRecord(
new NDEFRecordType(NDEFRecordType.UNKNOWN, “name”),
new String("F_M_NAME").getBytes(),
name.getBytes());
NDEFMessage message = new NDEFMessage(records);
conn.writeNDEF(message);
} catch (Exception e) { … }
}
This code transmits the first name in the PIM contacts list. First, it needs to ascertain
that name, which it does by using the PIM API (see Chapter 7) to obtain an instance of
the PIM manager. It uses the PIM manager to obtain the contact list and then uses the
enumeration the contact list provides to obtain the name stored in the first record of the
contact. Then the code creates a new NDEF record array, consisting of a single record.
This record is assigned a new NDEFRecord , of type unknown, bearing the ID F_M_NAME and
the formatted name obtained from the contact list. A new message is created using the
record array and then written via the connection.
Reading and writing to other contactless targets such as ISO 14443 is similar,
although instead of using separate read and write methods that the connector provides,
you exchange messages directly, as shown in Listing 15-11.
Listing 15-11. Exchanging Messages Directly
import java.io.IOException;
import javax.microedition.contactless.*;
import javax.microedition.contactless.sc.*;
import javax.microedition.io.Connector;
 
Search WWH ::




Custom Search