我只是想阅读标签的内容。但是我研究这个的时间越长,我就越觉得自己是唯一一个不明白它是如何工作的人。
浏览 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));
}
我还在清单和我自己的 XML 文件中添加了一些东西
显现:
<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