如何获取信息或Android程序programaticaly的NFC ID?

时间:2019-07-05 12:36:45

标签: android nfc

我在stackoverflow中尝试了一个som代码,但结果只是从卡中获取了nfc id,我想获取我的android的nfc id。谁能帮我

2 个答案:

答案 0 :(得分:0)

尝试一下:

val mTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

val id = byteArrayToHexString(mTag.getId());

答案 1 :(得分:0)

这是我的方法:

首先,您应该在此行中适当的时候初始化NfcAdapter

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

然后像这样覆盖onNewIntent

@Override
protected void onNewIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    Log.d(TAG, "onNewIntent: "+intent.getAction());

    if(tag != null) {
        Ndef ndef = Ndef.get(tag);
        readFromNFC(ndef);
    }else{
        toastUser(getApplicationContext(), "Problem reading NFC tag!\nPlease try again.", Toast.LENGTH_SHORT);
    }
}

最后阅读NFC标签

private void readFromNFC(Ndef ndef) {
    try {
        ndef.connect();
        NdefMessage ndefMessage = ndef.getNdefMessage();
        if(ndefMessage != null) {
            NdefRecord[] records = ndefMessage.getRecords();

            //records will produce an array of strings stored on the tag
            //iterate through them as handle as required.

        }
        ndef.close();

    } catch (IOException | FormatException e) {
        e.printStackTrace();
    }
}