gpt4 book ai didi

java - 如何使用 NFC 扫描标签?

转载 作者:行者123 更新时间:2023-11-30 03:39:50 25 4
gpt4 key购买 nike

我只是想读取标签的内容。但是我研究这个的时间越长,我就越觉得自己是唯一一个不理解它是如何工作的人。

翻看developer.android.com网站,发现了以下方法:

    public String readTag(Tag tag) {
MifareUltralight mifare = MifareUltralight.get(tag);
try {
mifare.connect();
byte[] payload = mifare.readPages(4);
return new String(payload, Charset.forName("US-ASCII"));
} catch (IOException e) {
Log.e(TAG, "IOException while writing Scanner message...", e);
} finally {
if (mifare != null) {
try {
mifare.close();
}
catch (IOException e) {
Log.e(TAG, "Error closing tag...", e);
}
}
}
return null;
}

现在,在我可以使用它之前,我需要初始化一些东西:

    public void configScanner()
{
Intent intent = getIntent();
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*"); /* Handles all MIME based dispatches. You should specify only the ones that you need. */
}
catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
intentFiltersArray = new IntentFilter[] {ndef, };
techListsArray = new String[][] { new String[] { Ndef.class.getName() } };
}

然后我只是尝试执行它。

public void scanClick(View v) 
{
textview1.setText(readTag(tag));
}

我还在 list 和我自己的 XML 文件中添加了一些东西

list :

<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/scan" />

扫描.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.NfcB</tech>
<tech>android.nfc.tech.NfcF</tech>
<tech>android.nfc.tech.NfcV</tech>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>

但是,当我执行代码时,我在执行 readTag(tag) 时崩溃了。我只是不明白如何让这些东西发挥作用。

根据要求:LogCat 输出:

04-11 17:13:07.650: E/AndroidRuntime(672): FATAL EXCEPTION: main
04-11 17:13:07.650: E/AndroidRuntime(672): java.lang.IllegalStateException: Could not execute method of the activity
04-11 17:13:07.650: E/AndroidRuntime(672): at android.view.View$1.onClick(View.java:3591)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.view.View.performClick(View.java:4084)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.view.View$PerformClick.run(View.java:16966)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.os.Handler.handleCallback(Handler.java:615)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.os.Looper.loop(Looper.java:137)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-11 17:13:07.650: E/AndroidRuntime(672): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 17:13:07.650: E/AndroidRuntime(672): at java.lang.reflect.Method.invoke(Method.java:511)
04-11 17:13:07.650: E/AndroidRuntime(672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-11 17:13:07.650: E/AndroidRuntime(672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-11 17:13:07.650: E/AndroidRuntime(672): at dalvik.system.NativeStart.main(Native Method)
04-11 17:13:07.650: E/AndroidRuntime(672): Caused by: java.lang.reflect.InvocationTargetException
04-11 17:13:07.650: E/AndroidRuntime(672): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 17:13:07.650: E/AndroidRuntime(672): at java.lang.reflect.Method.invoke(Method.java:511)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.view.View$1.onClick(View.java:3586)
04-11 17:13:07.650: E/AndroidRuntime(672): ... 11 more
04-11 17:13:07.650: E/AndroidRuntime(672): Caused by: java.lang.NullPointerException
04-11 17:13:07.650: E/AndroidRuntime(672): at android.nfc.tech.MifareUltralight.get(MifareUltralight.java:94)
04-11 17:13:07.650: E/AndroidRuntime(672): at com.example.android_test.MainActivity.readTag(MainActivity.java:108)
04-11 17:13:07.650: E/AndroidRuntime(672): at com.example.android_test.MainActivity.scanClick(MainActivity.java:155)
04-11 17:13:07.650: E/AndroidRuntime(672): ... 14 more

最佳答案

我在您发布的 list 部分没有看到这一点,但您确实需要它。

将此添加到您的 list

<uses-permission android:name="android.permission.NFC" />

关于java - 如何使用 NFC 扫描标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15955115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com