×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    Question tag detection isn't working

    Hi all,

    I've previously (successfully) developed applications which detect tags and read the data off them (and then perform some logic or other but that is besides the point).
    When it came to developing a new application, I decided to separate different types of logic so that the code becomes more re-usable.
    I've separated it into a midlet, a form, an NFCAgent class which performs all the NFC-related logic and a class which performs operations given the NDEF-records on the tags. For some reason, despite the code being practically copy-and-pasted from my previous applications, the targetDiscovered event is simply not being triggered.
    I'll post the code of the NFCAgent class so that maybe someone can help me, 'tis most confusing .

    Code:
    package NFC;
    
    import java.io.IOException;
    import java.util.Vector;
    import javax.microedition.contactless.*;
    import javax.microedition.contactless.ndef.*;
    import javax.microedition.io.Connector;
    
    public class NFCAgent implements TargetListener {
    
        private DiscoveryManager manager;
        private NDEFTagConnection connection;
        private NFCClient client;
        private boolean aborted = false;
    
        public NFCAgent(NFCClient client) {
            manager = DiscoveryManager.getInstance();
            try {
                manager.addTargetListener(this, TargetType.NDEF_TAG);
            } catch(ContactlessException ex) {
                client.NFCError("Unable to initiate discovery manager: " + ex.getMessage());
            } catch(IllegalStateException ex) {
                client.NFCError("Unable to initiate discovery manager: " + ex.getMessage());
            }
        }
    
        public void targetDetected(TargetProperties[] prop) {
            aborted = false;
            Vector records = new Vector();
            TargetProperties target = prop[0];
            try {
                connection = (NDEFTagConnection) Connector.open(target.getUrl());
            } catch (IOException ex) {
                client.NFCError("Unable to open connection to tag: " + ex.getMessage());
                abort();
            }
    
            if (!aborted) {
                try {
                    NDEFMessage msg = connection.readNDEF();
                    NDEFRecord[] rows = msg.getRecords();
                    for (int i = 0; i < rows.length; i++) {
                        NDEFRecord row = rows[i];
                        String str = new String(row.getPayload());
                        records.addElement(str);
                    }
                } catch (ContactlessException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
            client.targetRecordsReceived(records);
        }
    
        public void abort() {
            aborted = true;
            try {
                connection.close();
            } catch (IOException ignored) {
                //Nothing useful we can do here...
            }
        }
    
        public void close() {
            manager.removeTargetListener(this, TargetType.NDEF_TAG);
            try {
                connection.close();
            } catch (IOException ignored) {
                //Nothing useful we can do here...
            }
        }
    }
    and for the context, the NFCClient class:

    Code:
    package NFC;
    
    import java.util.Vector;
    
    public interface NFCClient {
        public void targetRecordsReceived(Vector records);
        public void NFCError(String message);
    }
    (I'm relatively new to Java, by the way, and have only been using it for about a week)

    Any help would be very much appreciated.
    Last edited by gfarrell; 2009-09-17 at 15:52. Reason: targetDiscovered not tagDiscovered

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    Unhappy Re: tag detection isn't working

    Bumping this a little. Please someone?

  3. #3
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: tag detection isn't working

    Quote Originally Posted by gfarrell View Post
    Bumping this a little. Please someone?
    Did you try with other NFC card ? Sometime there can be issue with one card. Let me know if you have also tried on other MIfair card.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    Re: tag detection isn't working

    Quote Originally Posted by honest_success View Post
    Did you try with other NFC card ? Sometime there can be issue with one card. Let me know if you have also tried on other MIfair card.
    I've only tried with one type of card (the tikitag/touchatag types) but the point is that the tag detection script that I used did work when it was all in one MIDlet class. As soon as logic was separated (as would be better form)into different classes that would perform specific operations, it started working. A simple copy-and-paste of the code back into the MIDlet (with some adjustments to remove some pointers) makes it work again.

  5. #5
    Regular Contributor
    Join Date
    Jul 2007
    Posts
    50

    Re: tag detection isn't working

    Are you sure the target listener is initialized (try adding a sysout after the addTargetListener, to see if it is).
    Is the targetDetected method triggered? (A sysout at the beginning of the method could help here too).

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    Re: tag detection isn't working

    Quote Originally Posted by Pybel View Post
    Are you sure the target listener is initialized (try adding a sysout after the addTargetListener, to see if it is).
    Is the targetDetected method triggered? (A sysout at the beginning of the method could help here too).
    I added various debugging methods and saw that the problem is that targetDetected is not called at all. Everything is initialised as it should be but that event is not triggered.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    3

    Re: tag detection isn't working

    Hi gfarrel, the same to me!!!
    do you have some suggestions??
    thanks
    Renato

Similar Threads

  1. <form> tag not working on xHTML-MP
    By rahul2435 in forum Browsing and Mark-ups
    Replies: 2
    Last Post: 2009-12-09, 14:01
  2. Replies: 2
    Last Post: 2009-08-27, 07:28
  3. How to format an NDEF tag
    By amarsahu in forum Near Field Communication
    Replies: 4
    Last Post: 2009-07-16, 11:24
  4. Deleting an image from the NDEF tag
    By amarsahu in forum Near Field Communication
    Replies: 1
    Last Post: 2009-06-30, 11:39
  5. Exception: Expected start tag found end tag
    By jjefrey in forum OMA DM/DS/CP
    Replies: 8
    Last Post: 2006-03-31, 07:55

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×