格式化的Mifare超轻C到NDEF [英] Formatting a Mifare Ultralight C to NDEF
问题描述
我要格式化的Mifare超轻C到处理NDEF消息。我已经写了下面的code和它的作品只是罚款与MIFARE经典芯片1K,但是当我尝试连接到超轻C个码片就得到一个IOExeption。
NdefFormatable格式= NdefFormatable.get(标签) 如果(格式!= NULL){
尝试{
format.connect();
format.format(新NdefMessage(新NdefRecord(NdefRecord.TNF_EMPTY,NULL,NULL,NULL)));
format.close();
Toast.makeText(getApplicationContext(),标签格式化,Toast.LENGTH_LONG).show();
}赶上(IOException异常五){
// TODO自动生成catch块
Toast.makeText(getApplicationContext(),无法连接,Toast.LENGTH_SHORT).show();
e.printStackTrace(); }赶上(FormatException E){
// TODO自动生成catch块
Toast.makeText(getApplicationContext(),无法格式化,Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
其他
Toast.makeText(getApplicationContext(),标签unformatable或已经格式化到NDEF,Toast.LENGTH_LONG).show();}
有谁知道什么错误可能是,为什么code工程为1K的芯片,而不是超轻C 2标签是全新的,完全空白。
这是logcat的输出:
8月4日至21日:49:27.300:W / System.err的(9351):java.io.IOException异常
8月4日至21日:49:27.300:W / System.err的(9351):在android.nfc.tech.NdefFormatable.format(NdefFormatable.java:132)
8月4日至21日:49:27.300:W / System.err的(9351):在android.nfc.tech.NdefFormatable.format(NdefFormatable.java:95)
8月4日至21日:49:27.300:W / System.err的(9351):在com.example.exjobb.nfc.FormatTag.formatTag(FormatTag.java:69)
8月4日至21日:49:27.300:W / System.err的(9351):在com.example.exjobb.nfc.FormatTag.onNewIntent(FormatTag.java:58
8月4日至21日:49:27.300:W / System.err的(9351):在android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1178)
8月4日至21日:49:27.300:W / System.err的(9351):在android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2418)
8月4日至21日:49:27.300:W / System.err的(9351):在android.app.ActivityThread.performNewIntents(ActivityThread.java:2431)
8月4日至21日:49:27.300:W / System.err的(9351):在android.app.ActivityThread.handleNewIntent(ActivityThread.java:2440)
8月4日至21日:49:27.300:W / System.err的(9351):在android.app.ActivityThread.access $ 1500(ActivityThread.java:159)
8月4日至21日:49:27.300:W / System.err的(9351):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1387)
8月4日至21日:49:27.300:W / System.err的(9351):在android.os.Handler.dispatchMessage(Handler.java:99)
8月4日至21日:49:27.300:W / System.err的(9351):在android.os.Looper.loop(Looper.java:176)
8月4日至21日:49:27.300:W / System.err的(9351):在android.app.ActivityThread.main(ActivityThread.java:5419)
8月4日至21日:49:27.300:W / System.err的(9351):在java.lang.reflect.Method.invokeNative(本机方法)
8月4日至21日:49:27.300:W / System.err的(9351):在java.lang.reflect.Method.invoke(Method.java:525)
8月4日至21日:49:27.305:W / System.err的(9351):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046)
8月4日至21日:49:27.305:W / System.err的(9351):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
8月4日至21日:49:27.305:W / System.err的(9351):在dalvik.system.NativeStart.main(本机方法)
我通过添加其他过滤器的过滤器ForegroundDispatch所做的工作格式化。之前,我是唯一的过滤功能标签发现,但加入它似乎是技术工作后才发现。
的IntentFilter tagDetected =新的IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter的techDetected =新的IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
IntentFilter的[] nfcIntentFilter =新的IntentFilter [] {tagDetected,techDetected};nfcAdapter.enableForegroundDispatch(这一点,nfcPendingIntent,nfcIntentFilter,NULL);
不要忘了新的过滤器添加到您的onNewIntent方法。
I want to format a Mifare Ultralight C to handle NDEF messages. I have written the code below and it works just fine with the Mifare Classic 1K chips but it gets an IOExeption when i try and connect to the Ultralight C chip.
NdefFormatable format = NdefFormatable.get(tag)
if(format != null){
try {
format.connect();
format.format(new NdefMessage(new NdefRecord(NdefRecord.TNF_EMPTY, null, null, null)));
format.close();
Toast.makeText(getApplicationContext(), "Tag formated.", Toast.LENGTH_LONG).show();
}catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Failed to connect", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (FormatException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Failed Format", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
else
Toast.makeText(getApplicationContext(), "Tag unformatable or already formatted to Ndef.", Toast.LENGTH_LONG).show();
}
Does anyone know what the error might be and why the code works for the 1k chips and not the Ultralight C? The tag is brand new and completely blank.
This is the output in logcat:
04-21 08:49:27.300: W/System.err(9351): java.io.IOException
04-21 08:49:27.300: W/System.err(9351): at android.nfc.tech.NdefFormatable.format(NdefFormatable.java:132)
04-21 08:49:27.300: W/System.err(9351): at android.nfc.tech.NdefFormatable.format(NdefFormatable.java:95)
04-21 08:49:27.300: W/System.err(9351): at com.example.exjobb.nfc.FormatTag.formatTag(FormatTag.java:69)
04-21 08:49:27.300: W/System.err(9351): at com.example.exjobb.nfc.FormatTag.onNewIntent(FormatTag.java:58
04-21 08:49:27.300: W/System.err(9351): at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1178)
04-21 08:49:27.300: W/System.err(9351): at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2418)
04-21 08:49:27.300: W/System.err(9351): at android.app.ActivityThread.performNewIntents(ActivityThread.java:2431)
04-21 08:49:27.300: W/System.err(9351): at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2440)
04-21 08:49:27.300: W/System.err(9351): at android.app.ActivityThread.access$1500(ActivityThread.java:159)
04-21 08:49:27.300: W/System.err(9351): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1387)
04-21 08:49:27.300: W/System.err(9351): at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 08:49:27.300: W/System.err(9351): at android.os.Looper.loop(Looper.java:176)
04-21 08:49:27.300: W/System.err(9351): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-21 08:49:27.300: W/System.err(9351): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 08:49:27.300: W/System.err(9351): at java.lang.reflect.Method.invoke(Method.java:525)
04-21 08:49:27.305: W/System.err(9351): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
04-21 08:49:27.305: W/System.err(9351): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-21 08:49:27.305: W/System.err(9351): at dalvik.system.NativeStart.main(Native Method)
I made the formating work by adding an other filter to the ForegroundDispatch filter. Before i was only filtering for a tag discovery but after adding Tech discovery it seems to be working.
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
IntentFilter[] nfcIntentFilter = new IntentFilter[]{tagDetected,techDetected};
nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, nfcIntentFilter, null);
Don't forget to add the new filter to your onNewIntent Method.
这篇关于格式化的Mifare超轻C到NDEF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!