android - 在 oncreate 方法中获取 NFC 唯一 ID

标签 android nfc uniqueidentifier oncreate

我正在尝试在 on create 方法中获取 NFC 标签的唯一 ID。我已经使用名为“onNewIntent”的覆盖方法成功完成了此操作,但是我想在 oncreate 方法中获取它。这是我尝试过的,我手机上的应用程序在启动时崩溃。请任何人帮忙。谢谢。

public class MainActivity extends Activity {

    TextView uid;

    private final String[][] techList = new String[][] {
        new String[] {
            NfcA.class.getName(),
            NfcB.class.getName(),
            NfcF.class.getName(),
            NfcV.class.getName(),
            NdefFormatable.class.getName(),
            TagTechnology.class.getName(),
            IsoDep.class.getName(),
            MifareClassic.class.getName(),
            MifareUltralight.class.getName(), 
            Ndef.class.getName()
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        uid = (TextView) findViewById(R.id.UID);
        Intent i = new Intent();
        IntentFilter filter = new IntentFilter();
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList);
        if (i.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
            ((TextView)findViewById(R.id.UID)).setText(ByteArrayToHexString(i.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
        }
    }

    private String ByteArrayToHexString(byte [] inarray) { //converts byte arrays to string
        int i, j, in;
        String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
        String out= "";

        for(j = 0 ; j < inarray.length ; ++j) 
            {
            in = (int) inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            out += hex[i];
            i = in & 0x0f;
            out += hex[i];
            }
        return out;
    }
}

最佳答案

Ok so basically i create a new intent. I get the default NFC hardware from the device. If a tag is discovered, it should change the textview to the ID of the NFC tag

要实现这一点,您所要做的就是设置新文本并调用 invalidate()onNewIntent() 中,以便使用新文本重新绘制 TextView:

package com.example.changetext;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView mNfcAdapterUid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNfcAdapterUid = (TextView)findViewById(R.id.nfc_adapter_uid);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
            mNfcAdapterUid.setText(ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
            mNfcAdapterUid.invalidate();
        }
    }

    private String ByteArrayToHexString(byte[] inarray) { // converts byte arrays to string
        int i, j, in;
        String[] hex = {
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
        };
        String out = "";

        for (j = 0; j < inarray.length; ++j) {
            in = inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            out += hex[i];
            i = in & 0x0f;
            out += hex[i];
        }
        return out;
    }
}

希望这有帮助。

关于android - 在 oncreate 方法中获取 NFC 唯一 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20462963/

相关文章:

android - 从 IntentService 调用时,React native 事件无法发出

android - 将数据写入新 NFC 标签不起作用?

javascript - 从 javascript 中的字符串数组创建唯一 ID?

javascript - 为每个设备生成唯一的ID

android - AltBeacon 应用程序在几分钟后重新启动

android - Google 登录失败 com.google.android.gms.common.api.ApiException : 10:

android - 以编程方式检查设备是否有 NFC 读取器

macos - 适用于 Mac OSX 的 ACR122U SDK

android - 完善除IMEI、Android_ID、WLAN Mac和蓝牙地址外设备的unique_id

android - 使 TextView 适合 Android 上的 Layout