Java Reference
In-Depth Information
sale or those used for secure identification mechanisms generally fall into this category)
or may only listen for a target while the application is active—for instance, once the user
constructs a message to send to a target.
To always listen for a contactless target, your application simply needs to register this
intent with the MIDP push registry. The API provides push notifications when NDEF-
enabled or contactless targets come into proximity with the Java ME device. To register
for a push notification when this occurs, you need to construct a URI indicating the type
of NDEF records the application seeks, and place this URI in a field of the JAD file with
the name
MIDlet-Push-
.
The URI can specify whether your application should start when the Java ME device
discovers a target containing either specific MIME-encoded data or a specific record
type. In either case, the push registry URI begins with the protocol
ndef
and then contin-
ues with either a MIME type declaration or record type declaration, like this:
n
ndef:mime?name=text/x-uri
This push registry URI will launch your application whenever the Java ME device
encounters an NDEF-enabled target bearing a URI (that is, a block of data with the MIME
type
text/x-uri
). Here's another example:
ndef:rtd?name=urn:nfc:wkt:Sp
In this example, the first two fields of the URI—
urn
and
nfc
—indicate that the record
is an NDEF record, while
wkt
indicates the namespace for the record. Finally,
Sp
indicates
that the target being sought is a Smart Poster.
If your application uses the registry to receive notification of proximate targets, it
should also implement the
javax.microedition.contactless.ndef.NDEFRecordListener
interface, which gives the platform a means to communicate data from the target to your
application. This method takes a single argument, an
NDEFRecord
generated by the target.
(I discuss these records in more detail in the next section.)
Once your application launches, you can also listen for targets by using the
DiscoveryManager
class, provided by the
javax.microedition.contactless
package.
Required by all Contactless Communication API-compliant devices, this interface lets
you enumerate which kinds of targets are supported, and it lets you register a listener
when the Java ME device encounters a specific target. The listener must implement
the
javax.microedition.contactless.TargetListener
interface, which defines a single
method,
targetDetected
, that the API invokes when the device encounters a new tar-
get. The API passes this method an array of
TargetProperties
that describes the target
the device has encountered. For example, Listing 15-8 registers a listener for NDEF-
enabled targets.