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 :confused:.
[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...
}
}
}
[/CODE]
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);
}
[/CODE]
(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.
Re: tag detection isn't working
Bumping this a little. Please someone?
Re: tag detection isn't working
[QUOTE=gfarrell;646676]Bumping this a little. Please someone?[/QUOTE]
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.
Re: tag detection isn't working
[QUOTE=honest_success;648224]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.[/QUOTE]
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.
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 [I]targetDetected [/I]method triggered? (A sysout at the beginning of the method could help here too).
Re: tag detection isn't working
[QUOTE=Pybel;648485]Are you sure the target listener is initialized (try adding a sysout after the addTargetListener, to see if it is).
Is the [I]targetDetected [/I]method triggered? (A sysout at the beginning of the method could help here too).[/QUOTE]
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.
Re: tag detection isn't working
Hi gfarrel, the same to me!!!
do you have some suggestions??
thanks
Renato