riks Posted May 31, 2022 at 02:16 PM Share Posted May 31, 2022 at 02:16 PM 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 Quote Link to comment Share on other sites More sharing options...
Superp Posted June 1, 2022 at 08:58 AM Share Posted June 1, 2022 at 08:58 AM 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? Quote Link to comment Share on other sites More sharing options...
photron Posted June 1, 2022 at 11:24 AM Share Posted June 1, 2022 at 11:24 AM 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(); } } Quote Link to comment Share on other sites More sharing options...
riks Posted June 2, 2022 at 07:25 AM Author Share Posted June 2, 2022 at 07:25 AM 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.