[英]Android NFC: Intent when detect any type of NFC
我試圖檢測何時讀取了NFC標簽。 該代碼有效,但不適用於Mifare Classic標簽。 Mi智能手機與此技術不兼容,但是我安裝了一個應用程序(NFC工具),該應用程序可以檢測Mifare Classic標簽何時在附近。 它無法讀取存儲的數據,但我不需要它。 我只需要檢測事件。 這個第三方應用程序可以做到,所以我敢肯定這是可能的,但我不知道該怎么做。
這是我的代碼:
private void prepareIntent() {
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (hasNfcCapabilities()) {
mPendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this, ((Object) this).getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter techDiscovered = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
IntentFilter tagDiscovered = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter ndefDiscovered = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
techDiscovered.addDataType("*/*");
tagDiscovered.addDataType("*/*");
ndefDiscovered.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
e.printStackTrace();
}
mFilters = new IntentFilter[]{techDiscovered, tagDiscovered, ndefDiscovered};
mTechLists = new String[][]{};
}
}
如何檢測Mifare Classic標簽?
謝謝。
解決了! 文檔說:
過濾IntentFilters以覆蓋其調度,或者將null始終調度
因此,在enableForegroundDispatch
的NfcAdapter
方法的filter參數中傳遞null
,它將檢測所有類型的NFC標簽。
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, mTechLists);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.