使用 nfc 工具的 clojure classcastexception 错误

clojure classcastexception bug with nfc-tools

我在 Clojure 中使用了来自 g运行did 的 NFC 库,但是当我尝试编写时遇到了一个严重的错误。在 Java 中有效:

public class TextWriter implements NdefOperationsListener {

@Override
public void onNdefOperations(NdefOperations ndefOperations) {
    System.out.println("Formated: " + ndefOperations.isFormatted() + " Writable: " + ndefOperations.isWritable());
    if (ndefOperations.isWritable()) {
        System.out.println("Writing NDEF data...");
        TextRecord record = new TextRecord("It works!");
        if (ndefOperations.isFormatted())
            ndefOperations.writeNdefMessage(record);
        else
            ndefOperations.format(record);
        System.out.println("Done");
    }
    else
        System.out.println("Tag not writable");
}

它从这段代码开始:

protected void launchDemo(NfcTagListener... listeners) throws IOException {
    NfcAdapter nfcAdapter = new NfcAdapter(TerminalUtils.getAvailableTerminal(), TerminalMode.INITIATOR, this);
    for (NfcTagListener tagListener : listeners)
        nfcAdapter.registerTagListener(tagListener);
    nfcAdapter.startListening();
    System.out.println("Waiting for tags, press ENTER to exit");
    System.in.read();
}

此代码由以下人员启动:

test.launchDemo(new MfClassicNfcTagListener(new TextWriter()));

总之这行得通。

clojure 代码:

(defn ndef-writer []
  (proxy [NdefOperationsListener] []
    (onNdefOperations [ndefOperations]
                  (println (str "Formatted: " (.isFormatted ndefOperations) " Writable: " (.isWritable ndefOperations)))
                  (if (.isWritable ndefOperations)
                    (do
                      (println "Writing NDEF data...")
                      (if (.isFormatted ndefOperations)
                        (.writeNdefMessage ndefOperations test-record)
                        (.format ndefOperations test-record)))
                    (println "Tag not writable")))

(defn write-demo []
  (doto @nfc-adapter (.registerTagListener (new MfClassicNfcTagListener    (ndef-writer))))
  (.startListening @nfc-adapter)
  (println "Waiting for tag..."))

(defn write-nfc-card []
   (try
     (write-demo)
   (catch Exception e (str "caught exception: " (.getMessage e)))))

但是当我 运行 clojure 代码时我得到这个错误:

java.lang.ClassCastException: Cannot cast org.nfctools.ndef.wkt.records.TextRecord to [Lorg.nfctools.ndef.Record;

at java.lang.Class.cast(Class.java:3258)

at clojure.lang.Reflector.boxArg(Reflector.java:427)

at clojure.lang.Reflector.boxArgs(Reflector.java:460)

at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:58)

at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:28)

at duva_desktop.nfc.write$ndef_writer$fn__6403.invoke(write.clj:48)

at duva_desktop.nfc.write.proxy$java.lang.Object$NdefOperationsListenerccf694.onNdefOperations(Unknown Source)

at org.nfctools.mf.classic.MfClassicNfcTagListener.handleTag(MfClassicNfcTagListener.java:54)

at org.nfctools.NfcAdapter.onTag(NfcAdapter.java:81)

at org.nfctools.spi.acs.InitiatorTerminalTagScanner.handleCard(InitiatorTerminalTagScanner.java:89)

at org.nfctools.spi.acs.InitiatorTerminalTagScanner.run(InitiatorTerminalTagScanner.java:55)

at java.lang.Thread.run(Thread.java:745)

我基本上编写了相同的代码,但后来在 Clojure 中它失败了,因为它无法将 TextRecord 转换为 Record(TextRecord 扩展了 WellKnownRecord,它扩展了 Record)(它不应该尝试转换吗?)

(instance? Record test-record) true

提前致谢!

p.s.

(def test-record
  (new TextRecord "it workedddd"))

我对 NFC 不熟悉,但错误消息中很好地解释了问题:

java.lang.ClassCastException: Cannot cast org.nfctools.ndef.wkt.records.TextRecord to [Lorg.nfctools.ndef.Record;

注意 class 名称前面的 L。 L 表示它是一个数组。您调用的方法很可能采用可变参数,在 java 中表示它是数组。因此,您的代码适用于 java,但不适用于 clojure。在 clojure 中,您必须显式传递数组,您可以这样做:

(into-array [test-record])