【问题标题】:Not Reading Nfc 4K Card details in Android未在 Android 中读取 Nfc 4K 卡详细信息
【发布时间】:2022-01-01 00:21:09
【问题描述】:

我想读取 NFC 4K 卡详细信息,它不读取数据,这是我使用的代码,

AndroidManifest.xml

<uses-permission android:name="android.permission.NFC" />
<uses-feature  android:name="android.hardware.nfc" android:required="true" />
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <action android:name="android.nfc.action.TECH_DISCOVERED" />
            <action android:name="android.nfc.action.TAG_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
        <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />
    </activity>

nfc_tech_filter.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>

MainActivity.java

public class MainActivity extends Activity implements NfcAdapter.ReaderCallback {
public static final String TAG = " --NFC-- " + MainActivity.class.getSimpleName();

private NfcAdapter nfcAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter == null) {
        showToast("No NFC found...");
    } else {
        showToast("NFC Enables Device...");
        enableReaderMode();
    }
}

@Override
public void onResume() {
    super.onResume();
    showToast("On Resume...");
    enableReaderMode();

}

@Override
protected void onPause() {
    super.onPause();
    showToast("On Pause...");
    disableReaderMode();
}


private void showToast(String message) {
    Log.d(TAG, message);
    try {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        showToast("Show Toast Exception : " + message);
    }
}


private void enableReaderMode() {
    showToast("Enabling reader mode...");
    Activity activity = MainActivity.this;
    nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
    if (nfcAdapter != null) {
        nfcAdapter.enableReaderMode(activity, this,  Integer.MAX_VALUE, null);
    }
}

private void disableReaderMode() {
    showToast("Disabling reader mode...");
    Activity activity = MainActivity.this;
    nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
    if (nfcAdapter != null) 
        nfcAdapter.disableReaderMode(activity);
}


@Override
public void onTagDiscovered(Tag tag) {
    Log.d(TAG, "New tag discovered...");
}
@Override
protected void onNewIntent(Intent intent) {
    Log.d(TAG, "New Intent...");
    super.onNewIntent(intent);
}

}

【问题讨论】:

    标签: android nfc card


    【解决方案1】:

    您没有在 onNewIntent() 中处理您的意图数据。每当您扫描 NFC 卡时,都会调用 onNewIntent()。 像下面的例子:

    @Override
    protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.d(TAG, "New Intent...");
    handleIntent(intent);
    }
    
    
    private void handleIntent(Intent intent) {
    val bundle = intent.extras
        if (bundle != null) {
            
    Log.e(TAG,intent.getParcelableExtra(NfcAdapter.EXTRA_TAG))
        }
    }
    

    已编辑

    添加适配器后需要从 onResume() 调用该方法。

    private fun setupForegroundDispatch(activity : Activity, adapter 
        : NfcAdapter) {
        val intent = Intent(activity.applicationContext, 
        activity.javaClass)
        intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
        val pendingIntent = 
        PendingIntent.getActivity(activity.applicationContext, 0, 
        intent, 0)
        //        val filters = arrayOfNulls<IntentFilter>(1)
        //        val techList = arrayOf<Array<String>>()
        val ndef = IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED)
        val filters = arrayOf(ndef)
        //        val techList = arrayOf<Array<String>>()
        val techList = arrayOf(arrayOf(NfcV::class.java.name))
    
        // Notice that this is the same filter as in our manifest.
        filters[0] = IntentFilter()
        filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED)
        filters[0].addCategory(Intent.CATEGORY_DEFAULT)
        try {
            filters[0].addDataType(MIME_TEXT_PLAIN)
        }
        catch (e : IntentFilter.MalformedMimeTypeException) {
            throw RuntimeException("Check your mime type.")
        }
        adapter.enableForegroundDispatch(activity, pendingIntent, 
       filters, techList)
    }
    

    如下:

    private void enableReaderMode() {
    showToast("Enabling reader mode...");
    Activity activity = MainActivity.this;
    nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
    if (nfcAdapter != null) {
        nfcAdapter.enableReaderMode(activity, this,  
     Integer.MAX_VALUE, null);
     setupForegroundDispatch(this,nfcAdapter);
    }
    }
    

    Follow this tutorial for more info

    【讨论】:

    • 我在那里添加了一个日志,它也没有显示在 Logcat 上..
    • 你得到新意图的日志了吗?
    • 它没有调用 onNewIntent() 方法
    • 我已经添加了我的方法。请检查答案
    【解决方案2】:

    当您使用enableReaderMode 时,您不需要onNewIntent 方法。

    清单中的intent-filter 和 nfc_tech_filter.xml 也可能不需要(如果您不希望应用程序在未运行的情况下由 NFC 标签启动,则不需要它们,那么您将处理 NFC Intent在onCreate)

    你没有做的是正确设置enableReaderMode

    
    @Override
        protected void onResume() {
            super.onResume();
    
            if(nfcAdapter!= null && nfcAdapter.isEnabled) {
                Bundle options = new Bundle();
                options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 250);
    
                nfcAdapter.enableReaderMode(this,
                        this,
                        NfcAdapter.FLAG_READER_NFC_A |
                                NfcAdapter.FLAG_READER_NFC_B |
                                NfcAdapter.FLAG_READER_NFC_F |
                                NfcAdapter.FLAG_READER_NFC_V |
                                NfcAdapter.FLAG_READER_NFC_BARCODE |
                                NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS,
                        options);
            }
    
        }
    

    如果您想生成自己的阅读通知而不是操作系统执行此操作,则关闭 NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS 是可选的。

    请注意,在写入 NFC 标签时,enableReaderMode 的较新 NFC API 为您提供了比旧 enableForegroundDispatch 更多的控制权,并且比 enableForegroundDispatch 更可靠,并且它没有 @ 的一些负面影响987654331@,所以我建议始终使用enableReaderMode,除非你想支持真正旧的 API

    另请注意,不建议使用 Mifare 4K 卡,因为它们是专有格式,并非所有 Android 硬件都支持(无论您使用哪种 API)

    【讨论】:

    • 如果他不使用 onNewIntent() 那么他在点击 NFC 时会在哪里接收数据?
    • @Nadim Ansari 数据通过更新更好的 API 在单独的线程中传递给 onTagDiscovered 方法。
    • 这也不起作用,我正在尝试从 4K 卡和 ATM 卡读取数据
    • 它没有调用 onTagDiscovered(Tag tag) 方法
    • 可能你的硬件不支持读取4k mifare卡,因为它们是非标准格式,但非接触式银行卡是标准格式,所以应该被检测到但不容易识别从他们那里读取任何数据,肯定不会有任何 ndef 数据。
    猜你喜欢
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 2011-06-02
    • 2021-07-27
    • 1970-01-01
    相关资源
    最近更新 更多