我在Clojure中使用了grundid的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$NdefOperationsListener$62ccf694.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"))
答案 0 :(得分:2)
我不熟悉NFC,但问题在错误消息中得到了很好的解释:
java.lang.ClassCastException: Cannot cast org.nfctools.ndef.wkt.records.TextRecord to [Lorg.nfctools.ndef.Record;
注意类名前面的L. L表示它是一个数组。你调用的方法主要是采用varargs,在java中意味着它是数组。因此,您的代码适用于java,但不适用于clojure。在clojure中你必须显式传递数组,你可以这样做:
(into-array [test-record])