Jump to content

Recommended Posts

Geschrieben

Hello guys,

I am trying out the standard java example of ExampleScanForTags. It scans a lot every second. Actually I just need when the ID changes and not if not. Is there some code that does that ?

Same way as the brick viewer shows it. Only when it changes.

Regards,
Rik

 

Geschrieben

ExampleScanForTags already uses callbacks, that's not the point here.

The point is that ExampleScanForTags just outputs every tag that was found, even if that tag doesn't change, it just repeats the output over and over again.

Here is a modified ExampleScanForTags that doesn't repeat the output if the tag didn't change:

import com.tinkerforge.IPConnection;
import com.tinkerforge.BrickletNFC;

public class ExampleScanForTags {
	private static final String HOST = "localhost";
	private static final int PORT = 4223;

	// Change XYZ to the UID of your NFC Bricklet
	private static final String UID = "X5a";

	// Note: To make the example code cleaner we do not handle exceptions. Exceptions
	//       you might normally want to catch are described in the documentation
	public static void main(String args[]) throws Exception {
		IPConnection ipcon = new IPConnection(); // Create IP connection
		// Note: Declare nfc as final, so the listener can access it
		final BrickletNFC nfc = new BrickletNFC(UID, ipcon); // Create device object

		ipcon.connect(HOST, PORT); // Connect to brickd
		// Don't use device before ipcon is connected

		// Add reader state changed listener
		nfc.addReaderStateChangedListener(new BrickletNFC.ReaderStateChangedListener() {
			private String lastTag = "";

			public void readerStateChanged(int state, boolean idle) {
				if(state == BrickletNFC.READER_STATE_REQUEST_TAG_ID_READY) {
					try {
						int i = 0;
						StringBuilder tagBuilder = new StringBuilder();
						BrickletNFC.ReaderGetTagID ret = nfc.readerGetTagID();

						for (int v: ret.tagID) {
							tagBuilder.append(String.format("0x%02X", v));

							if (i < ret.tagID.length - 1) {
								tagBuilder.append(" ");
							}

							i++;
						}

						String tag = tagBuilder.toString();

						if (!lastTag.equals(tag)) {
							System.out.format("Found tag of type %d with ID [%s]\n", ret.tagType, tag);

							lastTag = tag;
						}
					}
					catch (Exception e) {
						return;
					}
				}

				if (idle) {
					try {
						nfc.readerRequestTagID();
					}
					catch (Exception e) {
						return;
					}
				}
			}
		});

		// Enable reader mode
		nfc.setMode(BrickletNFC.MODE_READER);

		System.out.println("Press key to exit"); System.in.read();
		ipcon.disconnect();
	}
}

 

Geschrieben

Thanks Photron,

you are the best 😀.

Press key to exit
Found tag of type 2 with ID [0x04 0x66 0xD7 0xE2 0x1D 0x70 0x80]
Found tag of type 2 with ID [0x04 0x6B 0x4C 0x22 0xBC 0x73 0x81]
Found tag of type 0 with ID [0xAD 0x19 0xBE 0x49]

Regards,
Rik

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

×
×
  • Neu erstellen...