Jump to content

NFC Bricklet listener


riks

Recommended Posts

Hi Rik,

Regardless of the programming language, my advice would be to use a callback.

But the current documentation does not mention callbacks for this Bricklet and Java.

The docs mention callbacks for Java and other devices (example: Accelerometer Bricklet 2.0), and for the NFC Bricklet and another language like Ruby.

Maybe something is wrong with the documentation?

Link to comment
Share on other sites

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();
	}
}

 

Link to comment
Share on other sites

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...