Exchanging data with contactless targets

To communicate with contactless targets, the MIDlet must implement the TargetListener interface. The interface defines the targetDetected method, which is called every time the device detects one or more contactless targets whose type matches one of those for which the TargetListener is registered. The targetDetected method returns the properties of each detected contactless target in its TargetProperties[] parameter. You can use the properties to open connections to the targets.

To exchange data with a contactless target:

  1. Define the targetDetected method. Depending on your MIDlet implementation, you can define the method in the MIDlet main class, provided the main class implements TargetListener, or you can define the method in a custom class that implements TargetListener.

    The following code snippet creates the framework for the MyTargetListener custom class. The class is used for communicating with NDEF targets.

    // packages required for communicating with NDEF targets
    import java.io.IOException;
    import javax.microedition.contactless.ContactlessException;
    import javax.microedition.contactless.ndef.NDEFTagConnection;
    import javax.microedition.contactless.TargetListener;
    import javax.microedition.contactless.TargetProperties;
    import javax.microedition.contactless.TargetType;
    import javax.microedition.io.Connection;
    import javax.microedition.io.ConnectionNotFoundException;
    import javax.microedition.io.Connector;
    
    public class MyTargetListener implements TargetListener {
    
        public void targetDetected(TargetProperties[] properties) {
    
            // Retrieve information about the target (see step 2 of this example)
    
            // Connect to and exchange data with the target (see step 3 of this example)
    
            // Close connection to the target (see step 4 of this example)
    
        }
    
    } // end MyTargetListener
  2. Retrieve information about the detected contactless target by using the TargetProperties object of the targetDetected method.

    Note: On Symbian devices, if multiple contactless targets are detected, the targetDetected method only returns the properties of the first target detected. The first item in the TargetProperties[] parameter is therefore the only one to contain data.

    The following code snippet checks that the first contactless target found is an NDEF target, and retrieves the URL for connecting to the target.

    // select the first target found
    TargetProperties target = properties[0];
    
    String targetURL = "";
    
    TargetType[] targetType = target.getTargetTypes();
    if (targetType[0].equals(TargetType.NDEF_TAG)) {
        // if the target is an NDEF target,
        // retrieve its NDEFTagConnection URL
        targetURL = target.getUrl();
    } else {
        // if the target is not an NDEF_TAG target, exit the method
        return;
    }
  3. Open a TagConnection to the target, and use it to exchange data with the target. The specific type of connection to open depends on the type of the target. The Symbian implementation of the Contactless Communication API supports the following types of TagConnections:

    The following code snippet opens an NDEFTagConnection to an NDEF target and reads data from the target.

    try {
        // open an NDEFTagConnection to the target
        if (targetURL != null) {
            NDEFTagConnection connection = (NDEFTagConnection)Connector.open(targetURL);
            // Read data (NDEF records) from the target
            NDEFMessage message = connection.readNDEF();
            NDEFRecord[] records = message.getRecords();
            for (int i=0; i<records.length; i++) {
                // Handle the read data
            }
        } else {
            throw new ContactlessException("Target URL is invalid.");
        }
    } catch (IllegalArgumentException e) {
        // handle IllegalArgumentException from Connector.open()
    } catch (ConnectionNotFoundException e) {
        // handle ConnectionNotFoundException from Connector.open()
    } catch (SecurityException e) {
        // handle SecurityException from Connector.open()
    } catch (IOException e) {
        // handle IOException from Connector.open() or connection.readNDEF()
    } catch (IllegalStateException e) {
        // handle IllegalStateException from connection.readNDEF()
    } catch (ContactlessException e) {
        // handle ContactlessException from connection.readNDEF() or the else statement
    }
  4. After the TagConnection is no longer needed, close it by using the Connection.close method.

    try {
        connection.close();
    } catch (IOException e) {
        // handle IOException
    }